No description
  • TypeScript 98.7%
  • JavaScript 1.2%
  • Shell 0.1%
Find a file
Bortnikov Mikhail Evgenyevich 936d027a72 Merge branch 'feature/SAND-19-baobab' into 'feature/SAND-19'
Resolve SAND-19 "Feature/ baobab"

See merge request pd/web/packages/ext-plugins!4
2023-03-16 11:50:53 +00:00
.husky [SAND-18] feat: add husky 2023-03-06 12:49:17 +03:00
.vscode [SAND-6] fix: typing 3 2023-01-31 15:41:01 +03:00
scripts [SAND-18] feat: add husky 2023-03-06 12:49:17 +03:00
src [SAND-19] fix: docs 2023-03-16 09:16:50 +03:00
.editorconfig [SAND-6] feat: plugin etcd/version 2023-01-25 17:49:18 +03:00
.eslintrc [SAND-18] fix: doc bugs 2023-03-06 13:25:51 +03:00
.gitignore [SAND-6] feat: plugin etcd/version 2023-01-25 17:49:18 +03:00
.gitlab-ci.yml [SAND-6] feat: plugin etcd/version 2023-01-25 17:49:18 +03:00
.huskyrc.js [SAND-6] feat: plugin etcd/version 2023-01-25 17:49:18 +03:00
.npmrc [SAND-18] feat: add husky 2023-03-06 12:49:17 +03:00
.prettierrc.json [SAND-6] feat: plugin etcd/version 2023-01-25 17:49:18 +03:00
env.d.ts [SAND-6] feat: plugin etcd/version 2023-01-25 17:49:18 +03:00
package-lock.json [SAND-19] fix: dependency 2023-03-15 19:50:05 +03:00
package.json [SAND-19] fix: dependency 2023-03-15 19:50:05 +03:00
README.md [SAND-19] chore 2023-03-10 12:28:05 +03:00
tsconfig.json [SAND-19] fix: remove sopes and chore 2023-03-15 19:04:20 +03:00
tsconfig.node.json [SAND-19] fix: remove sopes and chore 2023-03-15 19:04:20 +03:00
vite.config.ts [SAND-19] fix: remove sopes and chore 2023-03-15 19:04:20 +03:00

PD WEB Plugins

Установка и подключение в проект

npm i @ozon-pd-web/plugins

Etcd

Создаём плагин ./plugins/etcd.ts

import type { InjectionKey } from 'vue';
import { inject } from 'vue';

import type { IEtcd } from '@ozon-pd-web/plugins/etcd';
import create from '@ozon-pd-web/plugins/etcd';

import type EtcdConfig from '@/../.local/etcd.json';
// Получим тип из .local/etcd
export type IEtcdConfig = typeof EtcdConfig.config;

export const EtcdKey: InjectionKey<IEtcd<IEtcdConfig>> = Symbol('etcd');

export const createEtcd = () =>
  create<IEtcdConfig>({
    pluginName: EtcdKey,
    refreshInterval: 2 * 60 * 1000,
  });

export const useEtcd = () => inject(EtcdKey) as IEtcd<IEtcdConfig>;

Подключаем плагин

import createEtcd from './plugins/etcd';
const etcd = await createEtcd();
app.use(etcd);

OR

await import('./plugins/etcd')
  .then(({ createEtcd }) => createEtcd())
  .then((etcd) => app.use(etcd));

Используем плагин

const { state: config } = useEtcd();
console.log(config.value.close)

Version

Создаём плагин ./plugins/version.ts

import type { InjectionKey } from 'vue';
import { inject } from 'vue';

import type { IVersion } from '@ozon-pd-web/plugins/version';
import create from '@ozon-pd-web/plugins/version';

import type VersionConfig from '@/../.local/version.json';
// Получим тип из .local/version
export type IVersionConfig = typeof VersionConfig;

export const VersionKey: InjectionKey<IVersion<IVersionConfig>> = Symbol('version');

export const createVersion = () =>
  create<IVersionConfig>({
    pluginName: VersionKey,
    refreshInterval: 2 * 60 * 1000,
  });

export const useVersion = () => inject(VersionKey) as IVersion<IVersionConfig>;

Подключаем плагин

import { createVersion } from './plugins/version';

const version = await createVersion();
app.use(version);

Используем плагин

const { state: env } = useVersion();
console.log(env.value);

Keycloak

Для инициализации рекомендуется использовать version

Создаём плагин ./plugins/keycloak.ts

import type { InjectionKey } from 'vue';
import { inject } from 'vue';

import type { IKeycloak } from '@ozon-pd-web/plugins/keycloak';
import create from '@ozon-pd-web/plugins/keycloak';

import type { IVersionConfig } from './version';

export const KeycloakKey: InjectionKey<IKeycloak> = Symbol('keycloak');

export const createKeycloak = (version: IVersionConfig) =>
  create(
    {
      pluginName: KeycloakKey,
      // refreshInterval: 2 * 60 * 1000,
    },
    {
      url: `${version.KEYCLOAK_ENDPOINT}/auth`,
      realm: version.REALMS,
      clientId: `k8s.${version.app_name}`,
    }
  );

export enum Roles {
  ADMIN = 'admin',
  USER = 'user',
}

export const useKeycloak = () => {
  const keycloak = inject(KeycloakKey) as IKeycloak;
  const { profile } = keycloak;

  function getName() {
    const { name, givenName, login } = profile.value;
    return name || givenName || login;
  }

  function getAvatar(size = 500) {
    return `https://nowatermark.ozone.ru/s3/staff-api/userspics/c${size}/${profile.value.login}.png`;
  }

  function getAllRoles() {
    return profile.value.roles as Array<Roles>;
  }

  function hasAnyRole(roles: Array<Roles>): boolean {
    const userRoles = getAllRoles();

    for (const role of roles) {
      if (userRoles.includes(role)) return true;
    }
    return false;
  }

  function getMaxRole(): Roles | undefined {
    const userRoles = getAllRoles();

    return [Roles.ADMIN, Roles.USER].find((role) => userRoles.includes(role));
  }
  return {
    profile,
    getName,
    getAvatar,
    getAllRoles,
    hasAnyRole,
    getMaxRole,
    logout: keycloak.logout,
  };
};

Подключаем плагин

import { createVersion } from './plugins/version';
import { createKeycloak } from './plugins/keycloak';

const version = await createVersion();
const keycloak = await createKeycloak(version.state);

app.use(version);
app.use(keycloak);

Используем плагин

const keycloak = useKeycloak();
console.log(keycloak.getName());

Logger

README ➡️