@pc-nexus/core 0.0.1-next.3 → 0.5.0-next.0
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 +4 -0
- package/index.d.ts.map +1 -1
- package/index.js +4 -0
- package/lib/code.d.ts +18 -0
- package/lib/code.d.ts.map +1 -0
- package/lib/code.js +19 -0
- package/lib/common.d.ts +6 -1
- package/lib/common.d.ts.map +1 -1
- package/lib/common.js +9 -4
- package/lib/context.d.ts +27 -9
- package/lib/context.d.ts.map +1 -1
- package/lib/context.js +5 -4
- package/lib/error-type.d.ts +11 -0
- package/lib/error-type.d.ts.map +1 -0
- package/lib/error-type.js +11 -0
- package/lib/error.d.ts +15 -0
- package/lib/error.d.ts.map +1 -0
- package/lib/error.js +23 -0
- package/lib/handler.d.ts +5 -12
- package/lib/handler.d.ts.map +1 -1
- package/lib/handler.js +1 -17
- package/lib/i18n/index.d.ts +3 -0
- package/lib/i18n/index.d.ts.map +1 -0
- package/lib/i18n/index.js +2 -0
- package/lib/i18n/main.d.ts +6 -0
- package/lib/i18n/main.d.ts.map +1 -0
- package/lib/i18n/main.js +42 -0
- package/lib/i18n/translation-loader.d.ts +8 -0
- package/lib/i18n/translation-loader.d.ts.map +1 -0
- package/lib/i18n/translation-loader.js +41 -0
- package/lib/i18n/types.d.ts +10 -0
- package/lib/i18n/types.d.ts.map +1 -0
- package/lib/i18n/types.js +1 -0
- package/lib/resolver.d.ts +4 -4
- package/lib/resolver.d.ts.map +1 -1
- package/lib/resolver.js +9 -1
- package/package.json +2 -1
package/index.d.ts
CHANGED
|
@@ -2,4 +2,8 @@ export * from "./lib/common.js";
|
|
|
2
2
|
export * from "./lib/context.js";
|
|
3
3
|
export * from "./lib/handler.js";
|
|
4
4
|
export * from "./lib/resolver.js";
|
|
5
|
+
export * from "./lib/error.js";
|
|
6
|
+
export * from "./lib/code.js";
|
|
7
|
+
export * from "./lib/error-type.js";
|
|
8
|
+
export * from "./lib/i18n/index.js";
|
|
5
9
|
//# 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,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC"}
|
package/index.js
CHANGED
|
@@ -2,3 +2,7 @@ export * from "./lib/common.js";
|
|
|
2
2
|
export * from "./lib/context.js";
|
|
3
3
|
export * from "./lib/handler.js";
|
|
4
4
|
export * from "./lib/resolver.js";
|
|
5
|
+
export * from "./lib/error.js";
|
|
6
|
+
export * from "./lib/code.js";
|
|
7
|
+
export * from "./lib/error-type.js";
|
|
8
|
+
export * from "./lib/i18n/index.js";
|
package/lib/code.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
declare enum NexusCode {
|
|
2
|
+
ok = 200,
|
|
3
|
+
invalidInput = 400,
|
|
4
|
+
unauthorized = 401,
|
|
5
|
+
forbidden = 403,
|
|
6
|
+
internalError = 500
|
|
7
|
+
}
|
|
8
|
+
export declare enum NexusHttpStatusCode {
|
|
9
|
+
ok = 200,
|
|
10
|
+
badRequest = 400,
|
|
11
|
+
unauthorized = 401,
|
|
12
|
+
notFound = 404,
|
|
13
|
+
methodNotAllowed = 405,
|
|
14
|
+
internalError = 500,
|
|
15
|
+
noContent = 204
|
|
16
|
+
}
|
|
17
|
+
export { NexusCode };
|
|
18
|
+
//# sourceMappingURL=code.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code.d.ts","sourceRoot":"","sources":["../../src/lib/code.ts"],"names":[],"mappings":"AAAA,aAAK,SAAS;IACV,EAAE,MAAM;IACR,YAAY,MAAM;IAClB,YAAY,MAAM;IAClB,SAAS,MAAM;IACf,aAAa,MAAM;CACtB;AAED,oBAAY,mBAAmB;IAC3B,EAAE,MAAM;IACR,UAAU,MAAM;IAChB,YAAY,MAAM;IAClB,QAAQ,MAAM;IACd,gBAAgB,MAAM;IACtB,aAAa,MAAM;IACnB,SAAS,MAAM;CAClB;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
package/lib/code.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
var NexusCode;
|
|
2
|
+
(function (NexusCode) {
|
|
3
|
+
NexusCode[NexusCode["ok"] = 200] = "ok";
|
|
4
|
+
NexusCode[NexusCode["invalidInput"] = 400] = "invalidInput";
|
|
5
|
+
NexusCode[NexusCode["unauthorized"] = 401] = "unauthorized";
|
|
6
|
+
NexusCode[NexusCode["forbidden"] = 403] = "forbidden";
|
|
7
|
+
NexusCode[NexusCode["internalError"] = 500] = "internalError";
|
|
8
|
+
})(NexusCode || (NexusCode = {}));
|
|
9
|
+
export var NexusHttpStatusCode;
|
|
10
|
+
(function (NexusHttpStatusCode) {
|
|
11
|
+
NexusHttpStatusCode[NexusHttpStatusCode["ok"] = 200] = "ok";
|
|
12
|
+
NexusHttpStatusCode[NexusHttpStatusCode["badRequest"] = 400] = "badRequest";
|
|
13
|
+
NexusHttpStatusCode[NexusHttpStatusCode["unauthorized"] = 401] = "unauthorized";
|
|
14
|
+
NexusHttpStatusCode[NexusHttpStatusCode["notFound"] = 404] = "notFound";
|
|
15
|
+
NexusHttpStatusCode[NexusHttpStatusCode["methodNotAllowed"] = 405] = "methodNotAllowed";
|
|
16
|
+
NexusHttpStatusCode[NexusHttpStatusCode["internalError"] = 500] = "internalError";
|
|
17
|
+
NexusHttpStatusCode[NexusHttpStatusCode["noContent"] = 204] = "noContent";
|
|
18
|
+
})(NexusHttpStatusCode || (NexusHttpStatusCode = {}));
|
|
19
|
+
export { NexusCode };
|
package/lib/common.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
+
import { NexusAppContext } from "./context.js";
|
|
1
2
|
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
3
|
export declare const invoke: (gateway: string, endpoint: string, payload: unknown) => Promise<Response>;
|
|
4
|
+
export declare const getAppContext: () => NexusAppContext;
|
|
5
|
+
export declare const I18nResourcesAccessor: {
|
|
6
|
+
getI18nInfoConfig: () => any;
|
|
7
|
+
getTranslationResource: (locale: string) => any;
|
|
8
|
+
};
|
|
4
9
|
//# sourceMappingURL=common.d.ts.map
|
package/lib/common.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/lib/common.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/lib/common.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAI/C,eAAO,MAAM,8BAA8B,0BAA0B,CAAC;AAEtE,eAAO,MAAM,MAAM,GAAI,SAAS,MAAM,EAAE,UAAU,MAAM,EAAE,SAAS,OAAO,KAAG,OAAO,CAAC,QAAQ,CACb,CAAC;AAEjF,eAAO,MAAM,aAAa,QAAO,eAEhC,CAAC;AAEF,eAAO,MAAM,qBAAqB;;qCAEG,MAAM;CAC1C,CAAC"}
|
package/lib/common.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
const GLOBAL_KEY_NEXUS_RUNTIME = "__nexus_runtime__";
|
|
1
2
|
export const NEXUS_CONTEXT_TOKEN_HEADER_KEY = "x-nexus-context-token";
|
|
2
|
-
export const
|
|
3
|
-
export const
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
export const invoke = (gateway, endpoint, payload) => global[GLOBAL_KEY_NEXUS_RUNTIME].invoke(gateway, endpoint, payload);
|
|
4
|
+
export const getAppContext = () => {
|
|
5
|
+
return global[GLOBAL_KEY_NEXUS_RUNTIME].getAppContext();
|
|
6
|
+
};
|
|
7
|
+
export const I18nResourcesAccessor = {
|
|
8
|
+
getI18nInfoConfig: () => global[GLOBAL_KEY_NEXUS_RUNTIME].i18n?.getI18nInfoConfig(),
|
|
9
|
+
getTranslationResource: (locale) => global[GLOBAL_KEY_NEXUS_RUNTIME].i18n?.getTranslationResource(locale),
|
|
10
|
+
};
|
package/lib/context.d.ts
CHANGED
|
@@ -13,24 +13,42 @@ export interface User {
|
|
|
13
13
|
timezone: string;
|
|
14
14
|
}
|
|
15
15
|
export interface Extension {
|
|
16
|
-
id: string;
|
|
17
16
|
environment_id: string;
|
|
18
17
|
environment_type: string;
|
|
19
18
|
installation_id: string;
|
|
20
19
|
}
|
|
21
|
-
export declare enum ExtensionType {
|
|
22
|
-
pjmProjectPage = "pjm:project-page"
|
|
23
|
-
}
|
|
24
20
|
export interface ExtensionData {
|
|
25
|
-
type:
|
|
21
|
+
type: string;
|
|
26
22
|
[key: string]: unknown;
|
|
27
23
|
}
|
|
24
|
+
export declare enum EnvironmentType {
|
|
25
|
+
development = "development",
|
|
26
|
+
production = "production"
|
|
27
|
+
}
|
|
28
28
|
export interface NexusContext {
|
|
29
|
+
teamId: string;
|
|
30
|
+
environmentId: string;
|
|
31
|
+
environmentName: string;
|
|
32
|
+
environmentType: EnvironmentType;
|
|
33
|
+
installationId: string;
|
|
34
|
+
}
|
|
35
|
+
export interface NexusAppContext extends NexusContext {
|
|
29
36
|
appId: string;
|
|
30
37
|
appVersion: string;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
38
|
+
invocationId: string;
|
|
39
|
+
extensionKey: string;
|
|
40
|
+
}
|
|
41
|
+
export type NexusExecContextUserType = "licensed" | "unlicensed" | "customer" | "anonymous";
|
|
42
|
+
export interface NexusExecContext extends NexusContext {
|
|
43
|
+
uid: string;
|
|
44
|
+
userType: NexusExecContextUserType;
|
|
45
|
+
localId: string;
|
|
46
|
+
installContext: string;
|
|
47
|
+
extension: ExtensionData;
|
|
48
|
+
}
|
|
49
|
+
export interface NexusRequestContext extends NexusExecContext, NexusAppContext {
|
|
50
|
+
functionsKeyHandlerMap: Record<string, string>;
|
|
35
51
|
}
|
|
52
|
+
export type NexusRequestContextRaw = Omit<NexusRequestContext, "invocationId" | "invocationRemainingTimeInMillis">;
|
|
53
|
+
export type NexusRequestEventContextRaw = Omit<NexusRequestContextRaw, "extension" | "extensionKey">;
|
|
36
54
|
//# sourceMappingURL=context.d.ts.map
|
package/lib/context.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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;
|
|
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,cAAc,EAAE,MAAM,CAAC;IAEvB,gBAAgB,EAAE,MAAM,CAAC;IAEzB,eAAe,EAAE,MAAM,CAAC;CAC3B;AAED,MAAM,WAAW,aAAa;IAC1B,IAAI,EAAE,MAAM,CAAC;IAEb,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CAC1B;AAED,oBAAY,eAAe;IACvB,WAAW,gBAAgB;IAE3B,UAAU,eAAe;CAC5B;AAED,MAAM,WAAW,YAAY;IACzB,MAAM,EAAE,MAAM,CAAC;IAEf,aAAa,EAAE,MAAM,CAAC;IAEtB,eAAe,EAAE,MAAM,CAAC;IAExB,eAAe,EAAE,eAAe,CAAC;IAEjC,cAAc,EAAE,MAAM,CAAC;CAC1B;AAED,MAAM,WAAW,eAAgB,SAAQ,YAAY;IACjD,KAAK,EAAE,MAAM,CAAC;IAEd,UAAU,EAAE,MAAM,CAAC;IAGnB,YAAY,EAAE,MAAM,CAAC;IAErB,YAAY,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,MAAM,wBAAwB,GAAG,UAAU,GAAG,YAAY,GAAG,UAAU,GAAG,WAAW,CAAC;AAE5F,MAAM,WAAW,gBAAiB,SAAQ,YAAY;IAClD,GAAG,EAAE,MAAM,CAAC;IAEZ,QAAQ,EAAE,wBAAwB,CAAC;IAEnC,OAAO,EAAE,MAAM,CAAC;IAGhB,cAAc,EAAE,MAAM,CAAC;IAEvB,SAAS,EAAE,aAAa,CAAC;CAC5B;AAED,MAAM,WAAW,mBAAoB,SAAQ,gBAAgB,EAAE,eAAe;IAC1E,sBAAsB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClD;AAED,MAAM,MAAM,sBAAsB,GAAG,IAAI,CAAC,mBAAmB,EAAE,cAAc,GAAG,iCAAiC,CAAC,CAAC;AAEnH,MAAM,MAAM,2BAA2B,GAAG,IAAI,CAAC,sBAAsB,EAAE,WAAW,GAAG,cAAc,CAAC,CAAC"}
|
package/lib/context.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export var
|
|
2
|
-
(function (
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export var EnvironmentType;
|
|
2
|
+
(function (EnvironmentType) {
|
|
3
|
+
EnvironmentType["development"] = "development";
|
|
4
|
+
EnvironmentType["production"] = "production";
|
|
5
|
+
})(EnvironmentType || (EnvironmentType = {}));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK 内部的错误类型码
|
|
3
|
+
*/
|
|
4
|
+
export declare enum NexusInnerErrorType {
|
|
5
|
+
invalidInput = "NEXUS_INNER_ERROR_TYPE_INVALID_INPUT",
|
|
6
|
+
networkInvalidInput = "NEXUS_INNER_ERROR_TYPE_NETWORK_INVALID_INPUT",
|
|
7
|
+
storageConditionBuilder = "NEXUS_INNER_ERROR_TYPE_STORAGE_CONDITION_BUILDER",
|
|
8
|
+
i18nTranslationLoaderError = "NEXUS_INNER_ERROR_TYPE_TRANSLATION_GETTER",
|
|
9
|
+
i18nTranslationLocaleInvalid = "NEXUS_INNER_ERROR_TYPE_TRANSLATION_LOCALE_INVALID"
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=error-type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-type.d.ts","sourceRoot":"","sources":["../../src/lib/error-type.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,mBAAmB;IAC3B,YAAY,yCAAyC;IACrD,mBAAmB,iDAAiD;IACpE,uBAAuB,qDAAqD;IAC5E,0BAA0B,8CAA8C;IACxE,4BAA4B,sDAAsD;CACrF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK 内部的错误类型码
|
|
3
|
+
*/
|
|
4
|
+
export var NexusInnerErrorType;
|
|
5
|
+
(function (NexusInnerErrorType) {
|
|
6
|
+
NexusInnerErrorType["invalidInput"] = "NEXUS_INNER_ERROR_TYPE_INVALID_INPUT";
|
|
7
|
+
NexusInnerErrorType["networkInvalidInput"] = "NEXUS_INNER_ERROR_TYPE_NETWORK_INVALID_INPUT";
|
|
8
|
+
NexusInnerErrorType["storageConditionBuilder"] = "NEXUS_INNER_ERROR_TYPE_STORAGE_CONDITION_BUILDER";
|
|
9
|
+
NexusInnerErrorType["i18nTranslationLoaderError"] = "NEXUS_INNER_ERROR_TYPE_TRANSLATION_GETTER";
|
|
10
|
+
NexusInnerErrorType["i18nTranslationLocaleInvalid"] = "NEXUS_INNER_ERROR_TYPE_TRANSLATION_LOCALE_INVALID";
|
|
11
|
+
})(NexusInnerErrorType || (NexusInnerErrorType = {}));
|
package/lib/error.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { NexusCode } from "./code.js";
|
|
2
|
+
import { NexusInnerErrorType } from "./error-type.js";
|
|
3
|
+
/**
|
|
4
|
+
* NexusError 异常类
|
|
5
|
+
*/
|
|
6
|
+
export declare abstract class NexusError extends Error {
|
|
7
|
+
#private;
|
|
8
|
+
get code(): NexusCode;
|
|
9
|
+
get type(): string;
|
|
10
|
+
constructor(code: NexusCode, type: string, message: string);
|
|
11
|
+
}
|
|
12
|
+
export declare class NexusInnerError extends NexusError {
|
|
13
|
+
constructor(code: NexusCode, type: NexusInnerErrorType, message: string);
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/lib/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD;;GAEG;AACH,8BAAsB,UAAW,SAAQ,KAAK;;IAE1C,IAAW,IAAI,IAAI,SAAS,CAE3B;IAGD,IAAW,IAAI,IAAI,MAAM,CAExB;gBAEkB,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAKpE;AAED,qBAAa,eAAgB,SAAQ,UAAU;gBACxB,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,mBAAmB,EAAE,OAAO,EAAE,MAAM;CAGjF"}
|
package/lib/error.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NexusError 异常类
|
|
3
|
+
*/
|
|
4
|
+
export class NexusError extends Error {
|
|
5
|
+
#code;
|
|
6
|
+
get code() {
|
|
7
|
+
return this.#code;
|
|
8
|
+
}
|
|
9
|
+
#type;
|
|
10
|
+
get type() {
|
|
11
|
+
return this.#type;
|
|
12
|
+
}
|
|
13
|
+
constructor(code, type, message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.#code = code;
|
|
16
|
+
this.#type = type;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class NexusInnerError extends NexusError {
|
|
20
|
+
constructor(code, type, message) {
|
|
21
|
+
super(code, type, message);
|
|
22
|
+
}
|
|
23
|
+
}
|
package/lib/handler.d.ts
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export interface HandlerFunctionEvent {
|
|
3
|
-
id: string;
|
|
1
|
+
import type { NexusExecContext } from "./context.js";
|
|
2
|
+
export interface HandlerFunctionEvent<T = unknown> {
|
|
4
3
|
type: string;
|
|
5
|
-
payload:
|
|
4
|
+
payload: T;
|
|
6
5
|
}
|
|
7
|
-
export interface HandlerFunction {
|
|
8
|
-
(context:
|
|
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>;
|
|
6
|
+
export interface HandlerFunction<TPayload = unknown, TReturn = void> {
|
|
7
|
+
(context: NexusExecContext, event: HandlerFunctionEvent<TPayload>): Promise<TReturn>;
|
|
15
8
|
}
|
|
16
9
|
//# sourceMappingURL=handler.d.ts.map
|
package/lib/handler.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/lib/handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/lib/handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD,MAAM,WAAW,oBAAoB,CAAC,CAAC,GAAG,OAAO;IAC7C,IAAI,EAAE,MAAM,CAAC;IAEb,OAAO,EAAE,CAAC,CAAC;CACd;AAED,MAAM,WAAW,eAAe,CAAC,QAAQ,GAAG,OAAO,EAAE,OAAO,GAAG,IAAI;IAC/D,CAAC,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,oBAAoB,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACxF"}
|
package/lib/handler.js
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
export
|
|
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
|
-
}
|
|
1
|
+
export {};
|
|
@@ -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,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":"AAIA,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"}
|
package/lib/i18n/main.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { NexusCode } from "../code.js";
|
|
2
|
+
import { NexusInnerErrorType } from "../error-type.js";
|
|
3
|
+
import { NexusInnerError } from "../error.js";
|
|
4
|
+
import { TranslationLoader } from "./translation-loader.js";
|
|
5
|
+
import { NexusSupportLocales } from "./types.js";
|
|
6
|
+
import _ from "lodash";
|
|
7
|
+
const translationsGetter = new TranslationLoader();
|
|
8
|
+
const ensureLocale = (rawLocale) => {
|
|
9
|
+
if (!NexusSupportLocales.includes(rawLocale)) {
|
|
10
|
+
throw new NexusInnerError(NexusCode.invalidInput, NexusInnerErrorType.i18nTranslationLocaleInvalid, `locale "${rawLocale}" is not supported`);
|
|
11
|
+
}
|
|
12
|
+
return rawLocale;
|
|
13
|
+
};
|
|
14
|
+
const translate = async (i18nKey, args, rawLocale) => {
|
|
15
|
+
let locale = undefined;
|
|
16
|
+
if (rawLocale) {
|
|
17
|
+
locale = ensureLocale(rawLocale);
|
|
18
|
+
}
|
|
19
|
+
const translationResult = await translationsGetter.getTranslations(locale);
|
|
20
|
+
if (!translationResult.translations) {
|
|
21
|
+
return i18nKey;
|
|
22
|
+
}
|
|
23
|
+
const result = _.get(translationResult.translations, i18nKey);
|
|
24
|
+
if (_.isString(result) && args) {
|
|
25
|
+
return result.replace(/\{\{\s*(.*?)\s*\}\}/g, (x, k) => {
|
|
26
|
+
const paramValue = _.get(args, k.trim());
|
|
27
|
+
return paramValue != null ? String(paramValue) : `{{${k}}}`;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return result || i18nKey;
|
|
31
|
+
};
|
|
32
|
+
const getTranslations = async (rawLocale) => {
|
|
33
|
+
let locale = undefined;
|
|
34
|
+
if (rawLocale) {
|
|
35
|
+
locale = ensureLocale(rawLocale);
|
|
36
|
+
}
|
|
37
|
+
return await translationsGetter.getTranslations(locale);
|
|
38
|
+
};
|
|
39
|
+
export const i18n = {
|
|
40
|
+
translate: translate,
|
|
41
|
+
getTranslations: getTranslations,
|
|
42
|
+
};
|
|
@@ -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/lib/resolver.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { NexusExecContext } from "./context.js";
|
|
2
2
|
export interface ResolverFunction<P, R> {
|
|
3
|
-
(context:
|
|
3
|
+
(context: NexusExecContext, payload: P): Promise<R>;
|
|
4
4
|
}
|
|
5
5
|
export declare class Resolver {
|
|
6
6
|
#private;
|
|
7
7
|
constructor();
|
|
8
|
-
define<P, R>(key: string, fn: ResolverFunction<P, R>): this;
|
|
9
|
-
invoke<P, R>(key: string, context:
|
|
8
|
+
define<P, R = unknown>(key: string, fn: ResolverFunction<P, R>): this;
|
|
9
|
+
invoke<P, R>(key: string, context: NexusExecContext, payload: P): Promise<R>;
|
|
10
10
|
}
|
|
11
11
|
//# sourceMappingURL=resolver.d.ts.map
|
package/lib/resolver.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolver.d.ts","sourceRoot":"","sources":["../../src/lib/resolver.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"resolver.d.ts","sourceRoot":"","sources":["../../src/lib/resolver.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAIrD,MAAM,WAAW,gBAAgB,CAAC,CAAC,EAAE,CAAC;IAClC,CAAC,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CACvD;AAQD,qBAAa,QAAQ;;;IAOV,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI;IAK/D,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAQ5F"}
|
package/lib/resolver.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
import { NexusCode } from "./code.js";
|
|
2
|
+
import { NexusError } from "./error.js";
|
|
3
|
+
import { NexusInnerErrorType } from "./error-type.js";
|
|
4
|
+
class ResolverError extends NexusError {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(NexusCode.invalidInput, NexusInnerErrorType.invalidInput, message);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
1
9
|
export class Resolver {
|
|
2
10
|
#functions;
|
|
3
11
|
constructor() {
|
|
@@ -10,7 +18,7 @@ export class Resolver {
|
|
|
10
18
|
async invoke(key, context, payload) {
|
|
11
19
|
const fn = this.#functions[key];
|
|
12
20
|
if (!fn) {
|
|
13
|
-
throw new
|
|
21
|
+
throw new ResolverError(`Cannot find function by key "${key}"`);
|
|
14
22
|
}
|
|
15
23
|
const result = await fn(context, payload);
|
|
16
24
|
return result;
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pc-nexus/core",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.5.0-next.0",
|
|
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"
|