@pc-nexus/core 0.1.0 → 0.5.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
+ export { EnvironmentType, NexusCode, NexusError, NexusHttpStatusCode, NexusInnerError, NexusInnerErrorType, Resolver, type Extension, type HandlerFunction, type HandlerFunctionEvent, type NexusAppContext, type ResolverFunction, type Team, type User, } from "@pc-nexus/internal";
1
2
  export * from "./lib/common.js";
2
- export * from "./lib/context.js";
3
- export * from "./lib/handler.js";
4
- export * from "./lib/resolver.js";
3
+ export * from "./lib/i18n/index.js";
5
4
  //# sourceMappingURL=index.d.ts.map
package/index.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,eAAe,EACf,SAAS,EACT,UAAU,EACV,mBAAmB,EACnB,eAAe,EACf,mBAAmB,EACnB,QAAQ,EACR,KAAK,SAAS,EACd,KAAK,eAAe,EACpB,KAAK,oBAAoB,EACzB,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,IAAI,EACT,KAAK,IAAI,GACZ,MAAM,oBAAoB,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC"}
package/index.js CHANGED
@@ -1,4 +1,3 @@
1
+ export { EnvironmentType, NexusCode, NexusError, NexusHttpStatusCode, NexusInnerError, NexusInnerErrorType, Resolver, } from "@pc-nexus/internal";
1
2
  export * from "./lib/common.js";
2
- export * from "./lib/context.js";
3
- export * from "./lib/handler.js";
4
- export * from "./lib/resolver.js";
3
+ export * from "./lib/i18n/index.js";
package/lib/common.d.ts CHANGED
@@ -1,4 +1,7 @@
1
- export declare const NEXUS_CONTEXT_TOKEN_HEADER_KEY = "x-nexus-context-token";
2
- export declare const NEXUS_GATEWAY_ENDPOINT_HEADER_KEY = "x-nexus-gateway-endpoint";
3
- export declare const invoke: (gateway: string, endpoint: string, payload: unknown) => Promise<Response>;
1
+ import type { NexusAppContext } from "@pc-nexus/internal";
2
+ export declare const getAppContext: () => NexusAppContext;
3
+ export declare const I18nResourcesAccessor: {
4
+ getI18nInfoConfig: () => any;
5
+ getTranslationResource: (locale: string) => any;
6
+ };
4
7
  //# sourceMappingURL=common.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/lib/common.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,8BAA8B,0BAA0B,CAAC;AACtE,eAAO,MAAM,iCAAiC,6BAA6B,CAAC;AAC5E,eAAO,MAAM,MAAM,GAAI,SAAS,MAAM,EAAE,UAAU,MAAM,EAAE,SAAS,OAAO,KAAG,OAAO,CAAC,QAAQ,CAClB,CAAC"}
1
+ {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/lib/common.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAE1D,eAAO,MAAM,aAAa,QAAO,eAEhC,CAAC;AAEF,eAAO,MAAM,qBAAqB;;qCAEG,MAAM;CAC1C,CAAC"}
package/lib/common.js CHANGED
@@ -1,5 +1,8 @@
1
- export const NEXUS_CONTEXT_TOKEN_HEADER_KEY = "x-nexus-context-token";
2
- export const NEXUS_GATEWAY_ENDPOINT_HEADER_KEY = "x-nexus-gateway-endpoint";
3
- export const invoke = (gateway, endpoint, payload) => global["__nexus_runtime__"].invoke(gateway, endpoint, payload);
4
- // const hook = (name: string): any => (global as any)[NEXUS_RUNTIME][name];
5
- // export { invoke, NEXUS_CONTEXT_TOKEN_HEADER_KEY, NEXUS_GATEWAY_ENDPOINT_HEADER_KEY };
1
+ import { GLOBAL_KEY_NEXUS_RUNTIME } from "@pc-nexus/internal";
2
+ export const getAppContext = () => {
3
+ return global[GLOBAL_KEY_NEXUS_RUNTIME].getAppContext();
4
+ };
5
+ export const I18nResourcesAccessor = {
6
+ getI18nInfoConfig: () => global[GLOBAL_KEY_NEXUS_RUNTIME].i18n?.getI18nInfoConfig(),
7
+ getTranslationResource: (locale) => global[GLOBAL_KEY_NEXUS_RUNTIME].i18n?.getTranslationResource(locale),
8
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./main.js";
2
+ export * from "./types.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/i18n/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./main.js";
2
+ export * from "./types.js";
@@ -0,0 +1,6 @@
1
+ import { TranslationResourceContent, TranslationResult } from "./types.js";
2
+ export declare const i18n: {
3
+ translate: (i18nKey: string, args?: Record<string, number | string>, rawLocale?: string) => Promise<string | TranslationResourceContent>;
4
+ getTranslations: (rawLocale?: string) => Promise<TranslationResult>;
5
+ };
6
+ //# sourceMappingURL=main.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../../src/lib/i18n/main.ts"],"names":[],"mappings":"AAEA,OAAO,EAA+B,0BAA0B,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAkDxG,eAAO,MAAM,IAAI;yBAhCJ,MAAM,SACR,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,cAC1B,MAAM,KACnB,OAAO,CAAC,MAAM,GAAG,0BAA0B,CAAC;kCAqBJ,MAAM,KAAG,OAAO,CAAC,iBAAiB,CAAC;CAW7E,CAAC"}
@@ -0,0 +1,40 @@
1
+ import { NexusCode, NexusInnerError, NexusInnerErrorType } from "@pc-nexus/internal";
2
+ import { TranslationLoader } from "./translation-loader.js";
3
+ import { NexusSupportLocales } from "./types.js";
4
+ import _ from "lodash";
5
+ const translationsGetter = new TranslationLoader();
6
+ const ensureLocale = (rawLocale) => {
7
+ if (!NexusSupportLocales.includes(rawLocale)) {
8
+ throw new NexusInnerError(NexusCode.invalidInput, NexusInnerErrorType.i18nTranslationLocaleInvalid, `locale "${rawLocale}" is not supported`);
9
+ }
10
+ return rawLocale;
11
+ };
12
+ const translate = async (i18nKey, args, rawLocale) => {
13
+ let locale = undefined;
14
+ if (rawLocale) {
15
+ locale = ensureLocale(rawLocale);
16
+ }
17
+ const translationResult = await translationsGetter.getTranslations(locale);
18
+ if (!translationResult.translations) {
19
+ return i18nKey;
20
+ }
21
+ const result = _.get(translationResult.translations, i18nKey);
22
+ if (_.isString(result) && args) {
23
+ return result.replace(/\{\{\s*(.*?)\s*\}\}/g, (x, k) => {
24
+ const paramValue = _.get(args, k.trim());
25
+ return paramValue != null ? String(paramValue) : `{{${k}}}`;
26
+ });
27
+ }
28
+ return result || i18nKey;
29
+ };
30
+ const getTranslations = async (rawLocale) => {
31
+ let locale = undefined;
32
+ if (rawLocale) {
33
+ locale = ensureLocale(rawLocale);
34
+ }
35
+ return await translationsGetter.getTranslations(locale);
36
+ };
37
+ export const i18n = {
38
+ translate: translate,
39
+ getTranslations: getTranslations,
40
+ };
@@ -0,0 +1,8 @@
1
+ import { Locale, TranslationResult } from "./types.js";
2
+ export declare class TranslationLoader {
3
+ #private;
4
+ constructor();
5
+ reset(): Promise<void>;
6
+ getTranslations(locale?: Locale): Promise<TranslationResult>;
7
+ }
8
+ //# sourceMappingURL=translation-loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"translation-loader.d.ts","sourceRoot":"","sources":["../../../src/lib/i18n/translation-loader.ts"],"names":[],"mappings":"AACA,OAAO,EAA8B,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAWnF,qBAAa,iBAAiB;;;IA4Bb,KAAK;IAKL,eAAe,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;CAa5E"}
@@ -0,0 +1,41 @@
1
+ import { I18nResourcesAccessor } from "../common.js";
2
+ export class TranslationLoader {
3
+ #i18nConfig;
4
+ #translationsMap;
5
+ constructor() {
6
+ this.#translationsMap = new Map();
7
+ }
8
+ async #getI18nConfig() {
9
+ if (this.#i18nConfig) {
10
+ return this.#i18nConfig;
11
+ }
12
+ this.#i18nConfig = await I18nResourcesAccessor.getI18nInfoConfig();
13
+ return this.#i18nConfig;
14
+ }
15
+ async #getTranslationResource(locale) {
16
+ let resource = this.#translationsMap.get(locale);
17
+ if (!resource) {
18
+ resource = await I18nResourcesAccessor.getTranslationResource(locale);
19
+ if (resource) {
20
+ this.#translationsMap.set(locale, resource);
21
+ }
22
+ }
23
+ return resource;
24
+ }
25
+ async reset() {
26
+ this.#i18nConfig = null;
27
+ this.#translationsMap.clear();
28
+ }
29
+ async getTranslations(locale) {
30
+ const config = await this.#getI18nConfig();
31
+ locale = locale || config.fallback.default;
32
+ const result = {
33
+ locale,
34
+ translations: null,
35
+ };
36
+ if (config.locales.includes(locale)) {
37
+ result.translations = await this.#getTranslationResource(locale);
38
+ }
39
+ return result;
40
+ }
41
+ }
@@ -0,0 +1,10 @@
1
+ export type Locale = "en-US" | "zh-CN";
2
+ export declare const NexusSupportLocales: Locale[];
3
+ export declare interface TranslationResult {
4
+ locale: Locale;
5
+ translations: TranslationResourceContent | null;
6
+ }
7
+ export interface TranslationResourceContent {
8
+ [key: string]: string | TranslationResourceContent;
9
+ }
10
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/i18n/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,GAAG,OAAO,GAAG,OAAO,CAAC;AAEvC,eAAO,MAAM,mBAAmB,EAAE,MAAM,EAAuB,CAAC;AAEhE,MAAM,CAAC,OAAO,WAAW,iBAAiB;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,0BAA0B,GAAG,IAAI,CAAC;CACnD;AAED,MAAM,WAAW,0BAA0B;IACvC,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,0BAA0B,CAAC;CACtD"}
@@ -0,0 +1 @@
1
+ export const NexusSupportLocales = ["en-US", "zh-CN"];
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@pc-nexus/core",
3
- "version": "0.1.0",
3
+ "version": "0.5.0-next.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "build": "tsc",
8
+ "test": "NODE_OPTIONS=\"--experimental-vm-modules --loader ts-node/esm\" jest",
8
9
  "pub:alpha": "cd dist && npm publish --access public --tag alpha",
9
10
  "pub": "cd dist && npm publish --access public --tag latest",
10
11
  "pub-next": "cd dist && npm publish --access public --tag next"
@@ -27,7 +28,10 @@
27
28
  }
28
29
  },
29
30
  "type": "module",
30
- "dependencies": {},
31
+ "dependencies": {
32
+ "@pc-nexus/internal": "0.5.0-next.1",
33
+ "lodash": "^4.17.21"
34
+ },
31
35
  "devDependencies": {},
32
36
  "peerDependencies": {}
33
37
  }
package/lib/context.d.ts DELETED
@@ -1,36 +0,0 @@
1
- export interface Team {
2
- _id: string;
3
- name: string;
4
- domain: string;
5
- locale: string;
6
- timezone: string;
7
- }
8
- export interface User {
9
- uid: string;
10
- name: string;
11
- display_name: string;
12
- locale: string;
13
- timezone: string;
14
- }
15
- export interface Extension {
16
- id: string;
17
- environment_id: string;
18
- environment_type: string;
19
- installation_id: string;
20
- }
21
- export declare enum ExtensionType {
22
- pjmProjectPage = "pjm:project-page"
23
- }
24
- export interface ExtensionData {
25
- type: ExtensionType;
26
- [key: string]: unknown;
27
- }
28
- export interface NexusContext {
29
- appId: string;
30
- appVersion: string;
31
- team: Team;
32
- user: User;
33
- extension: Extension;
34
- extensionData: ExtensionData;
35
- }
36
- //# sourceMappingURL=context.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/lib/context.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,IAAI;IACjB,GAAG,EAAE,MAAM,CAAC;IAEZ,IAAI,EAAE,MAAM,CAAC;IAEb,MAAM,EAAE,MAAM,CAAC;IAEf,MAAM,EAAE,MAAM,CAAC;IAEf,QAAQ,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,IAAI;IACjB,GAAG,EAAE,MAAM,CAAC;IAEZ,IAAI,EAAE,MAAM,CAAC;IAEb,YAAY,EAAE,MAAM,CAAC;IAErB,MAAM,EAAE,MAAM,CAAC;IAEf,QAAQ,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,SAAS;IACtB,EAAE,EAAE,MAAM,CAAC;IAEX,cAAc,EAAE,MAAM,CAAC;IAEvB,gBAAgB,EAAE,MAAM,CAAC;IAEzB,eAAe,EAAE,MAAM,CAAC;CAC3B;AAED,oBAAY,aAAa;IACrB,cAAc,qBAAqB;CACtC;AAED,MAAM,WAAW,aAAa;IAC1B,IAAI,EAAE,aAAa,CAAC;IAEpB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CAC1B;AAED,MAAM,WAAW,YAAY;IACzB,KAAK,EAAE,MAAM,CAAC;IAEd,UAAU,EAAE,MAAM,CAAC;IAEnB,IAAI,EAAE,IAAI,CAAC;IAEX,IAAI,EAAE,IAAI,CAAC;IAEX,SAAS,EAAE,SAAS,CAAC;IAErB,aAAa,EAAE,aAAa,CAAC;CAChC"}
package/lib/context.js DELETED
@@ -1,4 +0,0 @@
1
- export var ExtensionType;
2
- (function (ExtensionType) {
3
- ExtensionType["pjmProjectPage"] = "pjm:project-page";
4
- })(ExtensionType || (ExtensionType = {}));
package/lib/handler.d.ts DELETED
@@ -1,16 +0,0 @@
1
- import type { NexusContext } from "./context.js";
2
- export interface HandlerFunctionEvent {
3
- id: string;
4
- type: string;
5
- payload: unknown;
6
- }
7
- export interface HandlerFunction {
8
- (context: NexusContext, event: HandlerFunctionEvent): Promise<void>;
9
- }
10
- export declare class Handler {
11
- #private;
12
- constructor();
13
- define(key: string, fn: HandlerFunction): this;
14
- invoke(key: string, context: NexusContext, event: HandlerFunctionEvent): Promise<void>;
15
- }
16
- //# sourceMappingURL=handler.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/lib/handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD,MAAM,WAAW,oBAAoB;IACjC,EAAE,EAAE,MAAM,CAAC;IAEX,IAAI,EAAE,MAAM,CAAC;IAEb,OAAO,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,WAAW,eAAe;IAC5B,CAAC,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACvE;AAED,qBAAa,OAAO;;;IAOT,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,eAAe,GAAG,IAAI;IAKxC,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC;CAOtG"}
package/lib/handler.js DELETED
@@ -1,17 +0,0 @@
1
- export class Handler {
2
- #handlers;
3
- constructor() {
4
- this.#handlers = {};
5
- }
6
- define(key, fn) {
7
- this.#handlers[key] = fn;
8
- return this;
9
- }
10
- async invoke(key, context, event) {
11
- const fn = this.#handlers[key];
12
- if (!fn) {
13
- throw new Error(`Cannot find function by key "${key}"`);
14
- }
15
- await fn(context, event);
16
- }
17
- }
package/lib/resolver.d.ts DELETED
@@ -1,11 +0,0 @@
1
- import type { NexusContext } from "./context.js";
2
- export interface ResolverFunction<P, R> {
3
- (context: NexusContext, payload: P): Promise<R>;
4
- }
5
- export declare class Resolver {
6
- #private;
7
- constructor();
8
- define<P, R>(key: string, fn: ResolverFunction<P, R>): this;
9
- invoke<P, R>(key: string, context: NexusContext, payload: P): Promise<R>;
10
- }
11
- //# sourceMappingURL=resolver.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"resolver.d.ts","sourceRoot":"","sources":["../../src/lib/resolver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD,MAAM,WAAW,gBAAgB,CAAC,CAAC,EAAE,CAAC;IAClC,CAAC,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CACnD;AAED,qBAAa,QAAQ;;;IAOV,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI;IAKrD,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAQxF"}
package/lib/resolver.js DELETED
@@ -1,18 +0,0 @@
1
- export class Resolver {
2
- #functions;
3
- constructor() {
4
- this.#functions = {};
5
- }
6
- define(key, fn) {
7
- this.#functions[key] = fn;
8
- return this;
9
- }
10
- async invoke(key, context, payload) {
11
- const fn = this.#functions[key];
12
- if (!fn) {
13
- throw new Error(`Cannot find function by key "${key}"`);
14
- }
15
- const result = await fn(context, payload);
16
- return result;
17
- }
18
- }