awilixify 3.1.3 → 3.2.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/README.md +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/lib/extension-api/index.d.ts +7 -0
- package/dist/lib/extension-api/index.js +2 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -28
- package/dist/lib/codegen/cli.d.ts +0 -2
- package/dist/lib/codegen/cli.js +0 -57
- package/dist/lib/codegen/config.d.ts +0 -8
- package/dist/lib/codegen/config.js +0 -3
- package/dist/lib/codegen/generate-asyncapi-client.d.ts +0 -55
- package/dist/lib/codegen/generate-asyncapi-client.js +0 -359
- package/dist/lib/codegen/generate-orval-api-client.d.ts +0 -36
- package/dist/lib/codegen/generate-orval-api-client.js +0 -211
- package/dist/lib/codegen/index.d.ts +0 -3
- package/dist/lib/codegen/index.js +0 -3
- package/dist/lib/react/errors.d.ts +0 -12
- package/dist/lib/react/errors.js +0 -24
- package/dist/lib/react/index.d.ts +0 -4
- package/dist/lib/react/index.js +0 -4
- package/dist/lib/react/react-component-processor.d.ts +0 -10
- package/dist/lib/react/react-component-processor.js +0 -62
- package/dist/lib/react/react-di-context.d.ts +0 -16
- package/dist/lib/react/react-di-context.js +0 -52
- package/dist/lib/react/react-module-factories.d.ts +0 -6
- package/dist/lib/react/react-module-factories.js +0 -34
- package/dist/lib/react/react-module.types.d.ts +0 -169
- package/dist/lib/react/react-module.types.js +0 -1
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { mkdir, rename, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import { basename, dirname, extname, resolve } from "node:path";
|
|
4
|
-
const httpMethods = new Set([
|
|
5
|
-
"delete",
|
|
6
|
-
"get",
|
|
7
|
-
"head",
|
|
8
|
-
"options",
|
|
9
|
-
"patch",
|
|
10
|
-
"post",
|
|
11
|
-
"put",
|
|
12
|
-
"trace",
|
|
13
|
-
]);
|
|
14
|
-
export async function generateOrvalApiClient(options) {
|
|
15
|
-
const document = await resolveOpenApiDocument(options.openApi);
|
|
16
|
-
const operationIds = collectOperationIds(document, options.tags);
|
|
17
|
-
const getOperationFunctionName = options.getOperationFunctionName ?? ((operationId) => operationId);
|
|
18
|
-
assertIdentifier(options.apiClientClassName, "API client class name");
|
|
19
|
-
assertIdentifier(options.operationsConstName, "operations constant name");
|
|
20
|
-
const operations = operationIds.map((operationId) => {
|
|
21
|
-
assertIdentifier(operationId, "operation ID");
|
|
22
|
-
const functionName = getOperationFunctionName(operationId);
|
|
23
|
-
assertIdentifier(functionName, `function name for operation "${operationId}"`);
|
|
24
|
-
return { functionName, operationId };
|
|
25
|
-
});
|
|
26
|
-
const apiClientFileName = options.apiClientFileName ?? "generated-api-client.ts";
|
|
27
|
-
const operationsFileName = options.operationsFileName ?? "generated.operations.ts";
|
|
28
|
-
const outputDirectory = resolve(options.outputDirectory);
|
|
29
|
-
const apiClientPath = resolve(outputDirectory, apiClientFileName);
|
|
30
|
-
const operationsPath = resolve(outputDirectory, operationsFileName);
|
|
31
|
-
const moduleOptions = options.module;
|
|
32
|
-
const modulePath = moduleOptions
|
|
33
|
-
? resolve(moduleOptions.outputPath)
|
|
34
|
-
: undefined;
|
|
35
|
-
await mkdir(outputDirectory, { recursive: true });
|
|
36
|
-
await Promise.all([
|
|
37
|
-
replaceFile(apiClientPath, renderApiClient({
|
|
38
|
-
...options,
|
|
39
|
-
operations,
|
|
40
|
-
operationsImport: toJavaScriptImport(operationsFileName),
|
|
41
|
-
})),
|
|
42
|
-
replaceFile(operationsPath, renderOperations(options.serviceName, options.operationsConstName, operationIds)),
|
|
43
|
-
]);
|
|
44
|
-
const moduleCreated = moduleOptions && modulePath
|
|
45
|
-
? await createModuleIfMissing(modulePath, renderModule(options.apiClientClassName, moduleOptions))
|
|
46
|
-
: undefined;
|
|
47
|
-
return {
|
|
48
|
-
operationIds,
|
|
49
|
-
apiClientPath,
|
|
50
|
-
operationsPath,
|
|
51
|
-
modulePath,
|
|
52
|
-
moduleCreated,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
async function replaceFile(filePath, source) {
|
|
56
|
-
const temporaryPath = `${filePath}.${randomUUID()}.tmp`;
|
|
57
|
-
try {
|
|
58
|
-
await writeFile(temporaryPath, source);
|
|
59
|
-
await rename(temporaryPath, filePath);
|
|
60
|
-
}
|
|
61
|
-
finally {
|
|
62
|
-
await rm(temporaryPath, { force: true });
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
async function createModuleIfMissing(modulePath, source) {
|
|
66
|
-
await mkdir(dirname(modulePath), { recursive: true });
|
|
67
|
-
try {
|
|
68
|
-
await writeFile(modulePath, source, { flag: "wx" });
|
|
69
|
-
return true;
|
|
70
|
-
}
|
|
71
|
-
catch (error) {
|
|
72
|
-
if (typeof error === "object" &&
|
|
73
|
-
error !== null &&
|
|
74
|
-
"code" in error &&
|
|
75
|
-
error.code === "EEXIST") {
|
|
76
|
-
return false;
|
|
77
|
-
}
|
|
78
|
-
throw error;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
async function resolveOpenApiDocument(input) {
|
|
82
|
-
if (typeof input !== "string")
|
|
83
|
-
return input;
|
|
84
|
-
const response = await fetch(input);
|
|
85
|
-
if (!response.ok) {
|
|
86
|
-
throw new Error(`Failed to fetch OpenAPI document from ${input}: ${response.status} ${response.statusText}`);
|
|
87
|
-
}
|
|
88
|
-
return (await response.json());
|
|
89
|
-
}
|
|
90
|
-
function collectOperationIds(document, tags) {
|
|
91
|
-
const operationIds = new Set();
|
|
92
|
-
for (const pathItem of Object.values(document.paths ?? {})) {
|
|
93
|
-
for (const [method, operation] of Object.entries(pathItem)) {
|
|
94
|
-
if (!httpMethods.has(method.toLowerCase()))
|
|
95
|
-
continue;
|
|
96
|
-
if (typeof operation.operationId !== "string")
|
|
97
|
-
continue;
|
|
98
|
-
const operationTags = Array.isArray(operation.tags)
|
|
99
|
-
? operation.tags.filter((tag) => typeof tag === "string")
|
|
100
|
-
: [];
|
|
101
|
-
if (tags?.length && !tags.some((tag) => operationTags.includes(tag))) {
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
operationIds.add(operation.operationId);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return [...operationIds];
|
|
108
|
-
}
|
|
109
|
-
function renderApiClient({ apiClientClassName, generatedClientImport, operations, operationsConstName, operationsImport, }) {
|
|
110
|
-
const imports = operations
|
|
111
|
-
.map(({ functionName, operationId }) => `\t${functionName} as ${operationId}Request,`)
|
|
112
|
-
.join("\n");
|
|
113
|
-
const methods = operations
|
|
114
|
-
.map(({ operationId, }) => `\t@callsOperation(${operationsConstName}.${operationId})
|
|
115
|
-
\t${operationId}(
|
|
116
|
-
\t\t...args: Parameters<typeof ${operationId}Request>
|
|
117
|
-
\t): ReturnType<typeof ${operationId}Request> {
|
|
118
|
-
\t\treturn ${operationId}Request(
|
|
119
|
-
\t\t\t...(withTracePropagation(args, ${operationId}Request.length - 1) as Parameters<
|
|
120
|
-
\t\t\t\ttypeof ${operationId}Request
|
|
121
|
-
\t\t\t>),
|
|
122
|
-
\t\t);
|
|
123
|
-
\t}`)
|
|
124
|
-
.join("\n\n");
|
|
125
|
-
return `// Generated from OpenAPI. Do not edit.
|
|
126
|
-
import { callsOperation } from "awilixify";
|
|
127
|
-
import { getTracePropagationHeaders } from "awilixify/devtools";
|
|
128
|
-
|
|
129
|
-
import {
|
|
130
|
-
${imports}
|
|
131
|
-
} from ${JSON.stringify(generatedClientImport)};
|
|
132
|
-
import { ${operationsConstName} } from ${JSON.stringify(operationsImport)};
|
|
133
|
-
|
|
134
|
-
function withTracePropagation(args: unknown[], optionsIndex: number): unknown[] {
|
|
135
|
-
\tconst tracedArgs = [...args];
|
|
136
|
-
\tconst options = (tracedArgs[optionsIndex] ?? {}) as {
|
|
137
|
-
\t\theaders?: Record<string, string>;
|
|
138
|
-
\t};
|
|
139
|
-
|
|
140
|
-
\ttracedArgs[optionsIndex] = {
|
|
141
|
-
\t\t...options,
|
|
142
|
-
\t\theaders: {
|
|
143
|
-
\t\t\t...options.headers,
|
|
144
|
-
\t\t\t...getTracePropagationHeaders(),
|
|
145
|
-
\t\t},
|
|
146
|
-
\t};
|
|
147
|
-
|
|
148
|
-
\treturn tracedArgs;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export class ${apiClientClassName} {
|
|
152
|
-
${methods}
|
|
153
|
-
}
|
|
154
|
-
`;
|
|
155
|
-
}
|
|
156
|
-
function renderOperations(serviceName, operationsConstName, operationIds) {
|
|
157
|
-
const operations = Object.fromEntries(operationIds.map((operationId) => [
|
|
158
|
-
operationId,
|
|
159
|
-
{
|
|
160
|
-
serviceName,
|
|
161
|
-
operationId,
|
|
162
|
-
transport: "http",
|
|
163
|
-
},
|
|
164
|
-
]));
|
|
165
|
-
return `// Generated from OpenAPI. Do not edit.
|
|
166
|
-
export const ${operationsConstName} = ${JSON.stringify(operations, null, 2)} as const;
|
|
167
|
-
`;
|
|
168
|
-
}
|
|
169
|
-
function renderModule(apiClientClassName, options) {
|
|
170
|
-
assertIdentifier(options.moduleName, "module name");
|
|
171
|
-
assertIdentifier(options.apiClientProviderName, "API client provider name");
|
|
172
|
-
const exportNames = options.exports ?? [options.apiClientProviderName];
|
|
173
|
-
for (const providerName of exportNames) {
|
|
174
|
-
if (providerName !== options.apiClientProviderName) {
|
|
175
|
-
throw new Error(`Initial module export "${providerName}" is not the generated API client provider`);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
const imports = `import { createModule, type ModuleDef } from "awilixify";
|
|
179
|
-
|
|
180
|
-
import { ${apiClientClassName} } from ${JSON.stringify(options.apiClientImport)};`;
|
|
181
|
-
const exportKeys = exportNames.map((name) => JSON.stringify(name)).join(", ");
|
|
182
|
-
const moduleDefName = `${options.moduleName}Def`;
|
|
183
|
-
return `// Generated once by Awilixify codegen. Safe to customize.
|
|
184
|
-
${imports}
|
|
185
|
-
|
|
186
|
-
export type ${moduleDefName} = ModuleDef<{
|
|
187
|
-
\tproviders: {
|
|
188
|
-
\t\t${options.apiClientProviderName}: ${apiClientClassName};
|
|
189
|
-
\t};
|
|
190
|
-
\texportKeys: [${exportKeys}];
|
|
191
|
-
}>;
|
|
192
|
-
|
|
193
|
-
export type Deps = ${moduleDefName}["deps"];
|
|
194
|
-
|
|
195
|
-
export const ${options.moduleName} = createModule<${moduleDefName}>({
|
|
196
|
-
\tname: "${options.moduleName}",
|
|
197
|
-
\tproviders: {
|
|
198
|
-
\t\t${options.apiClientProviderName}: ${apiClientClassName},
|
|
199
|
-
\t},
|
|
200
|
-
\texports: [${exportKeys}],
|
|
201
|
-
});
|
|
202
|
-
`;
|
|
203
|
-
}
|
|
204
|
-
function assertIdentifier(value, label) {
|
|
205
|
-
if (!/^[$A-Z_a-z][$\w]*$/.test(value)) {
|
|
206
|
-
throw new Error(`${label} "${value}" is not a valid TypeScript identifier`);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
function toJavaScriptImport(fileName) {
|
|
210
|
-
return `./${basename(fileName, extname(fileName))}.js`;
|
|
211
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export declare class ComponentNameConflictError extends Error {
|
|
2
|
-
constructor(moduleName: string, conflictingKeys: string[]);
|
|
3
|
-
}
|
|
4
|
-
export declare class InvalidComponentExportError extends Error {
|
|
5
|
-
constructor(moduleName: string, componentKey: string);
|
|
6
|
-
}
|
|
7
|
-
export declare class ComponentDepsOverrideError extends Error {
|
|
8
|
-
constructor(componentKey: string);
|
|
9
|
-
}
|
|
10
|
-
export declare class ScopedProviderLifetimeError extends Error {
|
|
11
|
-
constructor(moduleName: string, providerKey: string);
|
|
12
|
-
}
|
package/dist/lib/react/errors.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export class ComponentNameConflictError extends Error {
|
|
2
|
-
constructor(moduleName, conflictingKeys) {
|
|
3
|
-
super(`Module "${moduleName}" has component name conflicts with providers or imported features: ${conflictingKeys.join(", ")}`);
|
|
4
|
-
this.name = "ComponentNameConflictError";
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
export class InvalidComponentExportError extends Error {
|
|
8
|
-
constructor(moduleName, componentKey) {
|
|
9
|
-
super(`Module "${moduleName}" exports component "${componentKey}", but that component is not declared by the module.`);
|
|
10
|
-
this.name = "InvalidComponentExportError";
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
export class ComponentDepsOverrideError extends Error {
|
|
14
|
-
constructor(componentKey) {
|
|
15
|
-
super(`Component "${componentKey}" received a "deps" prop. React component dependencies are injected by ReactDIContext and cannot be overridden from JSX.`);
|
|
16
|
-
this.name = "ComponentDepsOverrideError";
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
export class ScopedProviderLifetimeError extends Error {
|
|
20
|
-
constructor(moduleName, providerKey) {
|
|
21
|
-
super(`Provider "${providerKey}" in React module "${moduleName}" uses SCOPED lifetime. ReactDIContext supports SINGLETON and TRANSIENT providers only.`);
|
|
22
|
-
this.name = "ScopedProviderLifetimeError";
|
|
23
|
-
}
|
|
24
|
-
}
|
package/dist/lib/react/index.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import * as Awilix from "awilix";
|
|
2
|
-
import type { RegisteredModuleScope } from "../di/contexts/container-context-base.js";
|
|
3
|
-
import type { InternalModuleLike as M } from "../di/modules/runtime-module.types.js";
|
|
4
|
-
export declare class ReactComponentProcessor {
|
|
5
|
-
processComponents(m: M, scope: Awilix.AwilixContainer): void;
|
|
6
|
-
registerExportedComponents(scope: Awilix.AwilixContainer, importedModulesWithScope: RegisteredModuleScope[]): void;
|
|
7
|
-
getExportedComponentKeys(module: M): string[];
|
|
8
|
-
private wrapComponent;
|
|
9
|
-
private createDepsProxy;
|
|
10
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import * as Awilix from "awilix";
|
|
2
|
-
import { createElement } from "react";
|
|
3
|
-
import * as ERRORS from "./errors.js";
|
|
4
|
-
export class ReactComponentProcessor {
|
|
5
|
-
processComponents(m, scope) {
|
|
6
|
-
if (!m.components)
|
|
7
|
-
return;
|
|
8
|
-
const componentKeys = Object.keys(m.components);
|
|
9
|
-
const conflicts = componentKeys.filter((key) => scope.registrations[key]);
|
|
10
|
-
if (conflicts.length > 0) {
|
|
11
|
-
throw new ERRORS.ComponentNameConflictError(m.name, conflicts);
|
|
12
|
-
}
|
|
13
|
-
for (const [key, component] of Object.entries(m.components)) {
|
|
14
|
-
scope.register({
|
|
15
|
-
[key]: Awilix.asValue(this.wrapComponent(key, component, scope)),
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
registerExportedComponents(scope, importedModulesWithScope) {
|
|
20
|
-
for (const { module, moduleScope } of importedModulesWithScope) {
|
|
21
|
-
for (const key of module.componentExports || []) {
|
|
22
|
-
if (!module.components?.[key]) {
|
|
23
|
-
throw new ERRORS.InvalidComponentExportError(module.name, key);
|
|
24
|
-
}
|
|
25
|
-
if (scope.registrations[key]) {
|
|
26
|
-
throw new ERRORS.ComponentNameConflictError(module.name, [key]);
|
|
27
|
-
}
|
|
28
|
-
scope.register({
|
|
29
|
-
[key]: Awilix.asFunction(() => moduleScope.scope.resolve(key), {
|
|
30
|
-
lifetime: Awilix.Lifetime.TRANSIENT,
|
|
31
|
-
isLeakSafe: true,
|
|
32
|
-
}),
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
getExportedComponentKeys(module) {
|
|
38
|
-
return module.componentExports ? [...module.componentExports] : [];
|
|
39
|
-
}
|
|
40
|
-
wrapComponent(key, component, scope) {
|
|
41
|
-
return (props = {}) => {
|
|
42
|
-
if (Object.hasOwn(props, "deps")) {
|
|
43
|
-
throw new ERRORS.ComponentDepsOverrideError(key);
|
|
44
|
-
}
|
|
45
|
-
return createElement(component, {
|
|
46
|
-
...props,
|
|
47
|
-
deps: this.createDepsProxy(scope),
|
|
48
|
-
});
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
createDepsProxy(scope) {
|
|
52
|
-
return new Proxy({}, {
|
|
53
|
-
get: (_target, prop) => {
|
|
54
|
-
if (typeof prop !== "string")
|
|
55
|
-
return undefined;
|
|
56
|
-
if (!scope.registrations[prop])
|
|
57
|
-
return undefined;
|
|
58
|
-
return scope.resolve(prop);
|
|
59
|
-
},
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import * as Awilix from "awilix";
|
|
2
|
-
import { ContainerContextBase, type ContainerContextOptions, type ModuleScope, type RegisteredModuleScope } from "../di/contexts/container-context-base.js";
|
|
3
|
-
import type { InternalModuleLike as M } from "../di/modules/runtime-module.types.js";
|
|
4
|
-
import type { NonScopedResolverOptions } from "./react-module.types.js";
|
|
5
|
-
type ReactContainerContextOptions = ContainerContextOptions<NonScopedResolverOptions<any>>;
|
|
6
|
-
export declare class ReactDIContext extends ContainerContextBase {
|
|
7
|
-
private readonly componentProcessor;
|
|
8
|
-
private constructor();
|
|
9
|
-
static create(module: M, options?: ReactContainerContextOptions): ModuleScope;
|
|
10
|
-
protected registerImportedFeatures(scope: Awilix.AwilixContainer, importedModulesWithScope: RegisteredModuleScope[]): void;
|
|
11
|
-
protected ensureAdditionalNameConflicts(m: M, resolvedImports: M[]): void;
|
|
12
|
-
protected beforeRegisterProviders(m: M): void;
|
|
13
|
-
protected afterRegisterProviders(m: M, scope: Awilix.AwilixContainer): void;
|
|
14
|
-
private hasScopedProviderLifetime;
|
|
15
|
-
}
|
|
16
|
-
export {};
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import * as Awilix from "awilix";
|
|
2
|
-
import { ContainerContextBase, } from "../di/contexts/container-context-base.js";
|
|
3
|
-
import { ProviderResolver } from "../di/providers/provider-resolver.js";
|
|
4
|
-
import * as REACT_ERRORS from "./errors.js";
|
|
5
|
-
import { ReactComponentProcessor } from "./react-component-processor.js";
|
|
6
|
-
export class ReactDIContext extends ContainerContextBase {
|
|
7
|
-
componentProcessor = new ReactComponentProcessor();
|
|
8
|
-
constructor(options) {
|
|
9
|
-
super(options);
|
|
10
|
-
this.providerResolver = new ProviderResolver(undefined, this.options.providerOptions || {}, {});
|
|
11
|
-
}
|
|
12
|
-
static create(module, options) {
|
|
13
|
-
return new ReactDIContext(options ?? {}).bootstrapModule(module);
|
|
14
|
-
}
|
|
15
|
-
registerImportedFeatures(scope, importedModulesWithScope) {
|
|
16
|
-
this.registerExportedProviders(scope, importedModulesWithScope);
|
|
17
|
-
this.componentProcessor.registerExportedComponents(scope, importedModulesWithScope);
|
|
18
|
-
}
|
|
19
|
-
ensureAdditionalNameConflicts(m, resolvedImports) {
|
|
20
|
-
const componentKeys = Object.keys(m.components || {});
|
|
21
|
-
const providerConflicts = Object.keys(m.providers || {}).filter((key) => componentKeys.includes(key));
|
|
22
|
-
const importComponentConflicts = [
|
|
23
|
-
...this.globalModulesWithScope.flatMap(({ module: globalModule }) => this.componentProcessor.getExportedComponentKeys(globalModule)),
|
|
24
|
-
...resolvedImports.flatMap((importItem) => this.componentProcessor.getExportedComponentKeys(importItem)),
|
|
25
|
-
].filter((key) => componentKeys.includes(key));
|
|
26
|
-
const conflicts = [...providerConflicts, ...importComponentConflicts];
|
|
27
|
-
if (conflicts.length > 0) {
|
|
28
|
-
throw new REACT_ERRORS.ComponentNameConflictError(m.name, conflicts);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
beforeRegisterProviders(m) {
|
|
32
|
-
if (m.providerOptions?.lifetime === Awilix.Lifetime.SCOPED ||
|
|
33
|
-
this.options.providerOptions?.lifetime === Awilix.Lifetime.SCOPED) {
|
|
34
|
-
throw new REACT_ERRORS.ScopedProviderLifetimeError(m.name, "providerOptions");
|
|
35
|
-
}
|
|
36
|
-
for (const [key, provider] of Object.entries(m.providers || {})) {
|
|
37
|
-
if (this.hasScopedProviderLifetime(provider)) {
|
|
38
|
-
throw new REACT_ERRORS.ScopedProviderLifetimeError(m.name, key);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
afterRegisterProviders(m, scope) {
|
|
43
|
-
this.componentProcessor.processComponents(m, scope);
|
|
44
|
-
}
|
|
45
|
-
hasScopedProviderLifetime(provider) {
|
|
46
|
-
return (typeof provider === "object" &&
|
|
47
|
-
provider !== null &&
|
|
48
|
-
("useClass" in provider || "useFactory" in provider) &&
|
|
49
|
-
"lifetime" in provider &&
|
|
50
|
-
provider.lifetime === Awilix.Lifetime.SCOPED);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { Module, ModuleDefinition } from "./react-module.types.js";
|
|
2
|
-
export type ModuleOptions = {
|
|
3
|
-
hashNameFrom?: unknown;
|
|
4
|
-
hashLength?: number;
|
|
5
|
-
};
|
|
6
|
-
export declare function createModule<TDef extends ModuleDefinition>(module: Module<TDef>, options?: ModuleOptions): Module<TDef>;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
export function createModule(module, options) {
|
|
2
|
-
if (options?.hashNameFrom === undefined) {
|
|
3
|
-
return module;
|
|
4
|
-
}
|
|
5
|
-
const hash = hashString(stableStringify(options.hashNameFrom)).slice(0, Math.max(4, options.hashLength ?? 8));
|
|
6
|
-
return {
|
|
7
|
-
...module,
|
|
8
|
-
name: `${module.name}_${hash}`,
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
function hashString(value) {
|
|
12
|
-
let hash = 5381;
|
|
13
|
-
for (let index = 0; index < value.length; index += 1) {
|
|
14
|
-
hash = (hash * 33) ^ value.charCodeAt(index);
|
|
15
|
-
}
|
|
16
|
-
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
17
|
-
}
|
|
18
|
-
function stableStringify(value) {
|
|
19
|
-
return JSON.stringify(sortRecursively(value));
|
|
20
|
-
}
|
|
21
|
-
function sortRecursively(value) {
|
|
22
|
-
if (Array.isArray(value)) {
|
|
23
|
-
return value.map(sortRecursively);
|
|
24
|
-
}
|
|
25
|
-
if (value && typeof value === "object") {
|
|
26
|
-
return Object.keys(value)
|
|
27
|
-
.sort()
|
|
28
|
-
.reduce((acc, key) => {
|
|
29
|
-
acc[key] = sortRecursively(value[key]);
|
|
30
|
-
return acc;
|
|
31
|
-
}, {});
|
|
32
|
-
}
|
|
33
|
-
return value;
|
|
34
|
-
}
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import type { BuildResolverOptions, ContainerOptions, LifetimeType } from "awilix";
|
|
2
|
-
import type { EmptyObject } from "../di/common.types.js";
|
|
3
|
-
import type { ModuleImport } from "../di/modules/module.types.js";
|
|
4
|
-
import type { GlobalDependencies, NormalizeGlobalDependencies } from "../di/modules/module-def.types.js";
|
|
5
|
-
import type { DefProviderMap, Provider } from "../di/providers/provider.types.js";
|
|
6
|
-
export type ReactComponent<TProps = any, TResult = any> = (props: TProps) => TResult;
|
|
7
|
-
declare const reactImportModuleDefMarker: unique symbol;
|
|
8
|
-
export type ReactImportModule<TDef extends ModuleDefinition = ModuleDefinition> = {
|
|
9
|
-
name: string;
|
|
10
|
-
[reactImportModuleDefMarker]?: TDef;
|
|
11
|
-
};
|
|
12
|
-
export type ReactModuleImport = ModuleImport | ReactImportModule<any>;
|
|
13
|
-
declare const reactComponentPropsMarker: unique symbol;
|
|
14
|
-
export type WithDeps<TProps, TDeps = Record<string, unknown>> = TProps & {
|
|
15
|
-
readonly deps: TDeps;
|
|
16
|
-
readonly [reactComponentPropsMarker]?: TProps;
|
|
17
|
-
};
|
|
18
|
-
export type WithDepsOnly<TDeps = Record<string, unknown>> = WithDeps<Record<string, never>, TDeps>;
|
|
19
|
-
type PropsWithoutDeps<TProps> = TProps extends {
|
|
20
|
-
readonly [reactComponentPropsMarker]?: infer TPublicProps;
|
|
21
|
-
} ? TPublicProps : Omit<TProps, "deps">;
|
|
22
|
-
export type WithoutDeps<TComponent> = TComponent extends (props: infer TProps) => infer TResult ? (props: PropsWithoutDeps<TProps>) => TResult : never;
|
|
23
|
-
export type WithoutDepsComponentMap<TComponents> = TComponents extends Record<string, ReactComponent> ? {
|
|
24
|
-
[K in keyof TComponents]: WithoutDeps<TComponents[K]>;
|
|
25
|
-
} : EmptyObject;
|
|
26
|
-
type HasDuplicates<T extends readonly unknown[]> = number extends T["length"] ? false : T extends readonly [infer Head, ...infer Tail] ? Head extends Tail[number] ? true : HasDuplicates<Tail> : false;
|
|
27
|
-
type KeysOf<T> = Extract<keyof T, string>;
|
|
28
|
-
type ValidExportKeys<TMap, TKeys> = [TMap] extends [Record<string, unknown>] ? TKeys extends readonly KeysOf<TMap>[] ? HasDuplicates<TKeys> extends true ? never : TKeys : readonly KeysOf<TMap>[] : never;
|
|
29
|
-
export type ModuleDef<D extends {
|
|
30
|
-
providers?: DefProviderMap;
|
|
31
|
-
components?: Record<string, ReactComponent>;
|
|
32
|
-
imports?: readonly ReactModuleImport[];
|
|
33
|
-
exportKeys?: ValidExportKeys<D["providers"], D["exportKeys"]>;
|
|
34
|
-
componentExportKeys?: ValidExportKeys<D["components"], D["componentExportKeys"]>;
|
|
35
|
-
}> = {
|
|
36
|
-
providers: ExtractProviders<D["providers"]>;
|
|
37
|
-
exports: ExtractExports<ExtractProviders<D["providers"]>, D["exportKeys"]>;
|
|
38
|
-
components: ExtractComponents<D["components"]>;
|
|
39
|
-
componentExports: ExtractComponentExports<ExtractComponents<D["components"]>, D["componentExportKeys"]>;
|
|
40
|
-
imports: ExtractImports<D["imports"]>;
|
|
41
|
-
providerDeps: ExtractProviderDeps<D>;
|
|
42
|
-
deps: ExtractComponentDeps<D>;
|
|
43
|
-
exportKeys: ExtractExportKeys<ExtractProviders<D["providers"]>, D["exportKeys"]>;
|
|
44
|
-
componentExportKeys: ExtractComponentExportKeys<ExtractComponents<D["components"]>, D["componentExportKeys"]>;
|
|
45
|
-
};
|
|
46
|
-
export type InferComponentDeps<TModuleDef extends {
|
|
47
|
-
deps: object;
|
|
48
|
-
}> = TModuleDef["deps"];
|
|
49
|
-
export type InferProviderDeps<TModuleDef extends {
|
|
50
|
-
providerDeps: object;
|
|
51
|
-
}> = TModuleDef["providerDeps"];
|
|
52
|
-
type NormalizeModuleDef<Def extends ModuleDefinition> = {
|
|
53
|
-
providers: Def["providers"] extends DefProviderMap ? Def["providers"] : EmptyObject;
|
|
54
|
-
exports: Def["exports"] extends DefProviderMap ? Def["exports"] : EmptyObject;
|
|
55
|
-
components: Def["components"] extends Record<string, ReactComponent> ? Def["components"] : EmptyObject;
|
|
56
|
-
componentExports: Def["componentExports"] extends Record<string, ReactComponent> ? Def["componentExports"] : EmptyObject;
|
|
57
|
-
imports: Def["imports"] extends readonly ReactModuleImport[] ? Def["imports"] : readonly [];
|
|
58
|
-
deps: Def["deps"] extends object ? Def["deps"] : EmptyObject;
|
|
59
|
-
providerDeps: Def["providerDeps"] extends object ? Def["providerDeps"] : EmptyObject;
|
|
60
|
-
exportKeys: Def["exportKeys"] extends readonly string[] ? Def["exportKeys"] : readonly [];
|
|
61
|
-
componentExportKeys: Def["componentExportKeys"] extends readonly string[] ? Def["componentExportKeys"] : readonly [];
|
|
62
|
-
};
|
|
63
|
-
export type Module<Def extends ModuleDefinition> = ModuleFromDef<NormalizeModuleDef<Def>> & ReactImportModule<Def>;
|
|
64
|
-
type ModuleFromDef<Def extends {
|
|
65
|
-
providers: DefProviderMap | EmptyObject;
|
|
66
|
-
components: Record<string, ReactComponent> | EmptyObject;
|
|
67
|
-
imports: readonly ReactModuleImport[];
|
|
68
|
-
deps: object;
|
|
69
|
-
providerDeps: object;
|
|
70
|
-
exportKeys: readonly string[];
|
|
71
|
-
componentExportKeys: readonly string[];
|
|
72
|
-
}> = {
|
|
73
|
-
name: string;
|
|
74
|
-
containerOptions?: ContainerOptions;
|
|
75
|
-
providerOptions?: NonScopedResolverOptions<any>;
|
|
76
|
-
} & WithProviders<Def["providers"], Def["providerDeps"]> & WithImports<Def["imports"]> & WithExports<Def["exportKeys"]> & WithComponents<Def["components"]> & WithComponentExports<Def["componentExportKeys"]>;
|
|
77
|
-
export type ModuleDefinition = {
|
|
78
|
-
providers?: DefProviderMap;
|
|
79
|
-
exports?: DefProviderMap;
|
|
80
|
-
components?: Record<string, ReactComponent>;
|
|
81
|
-
componentExports?: Record<string, ReactComponent>;
|
|
82
|
-
imports?: ReactModuleImport[];
|
|
83
|
-
deps?: object;
|
|
84
|
-
providerDeps?: object;
|
|
85
|
-
exportKeys?: readonly string[];
|
|
86
|
-
componentExportKeys?: readonly string[];
|
|
87
|
-
};
|
|
88
|
-
type ExtractProviders<TProviders> = TProviders extends DefProviderMap ? TProviders : EmptyObject;
|
|
89
|
-
type ExtractComponents<TComponents> = TComponents extends Record<string, ReactComponent> ? TComponents : EmptyObject;
|
|
90
|
-
type ExtractImports<TImports> = TImports extends readonly ReactModuleImport[] ? TImports : [];
|
|
91
|
-
type ExtractExports<TProviders, TKeys> = TProviders extends DefProviderMap ? TKeys extends readonly (keyof TProviders)[] ? Pick<TProviders, TKeys[number]> : EmptyObject : EmptyObject;
|
|
92
|
-
type ExtractExportKeys<TProviders, TKeys> = TProviders extends DefProviderMap ? TKeys extends readonly (keyof TProviders)[] ? TKeys : readonly [] : readonly [];
|
|
93
|
-
type ExtractComponentExports<TComponents, TKeys> = TComponents extends Record<string, ReactComponent> ? TKeys extends readonly (keyof TComponents)[] ? Pick<TComponents, TKeys[number]> : EmptyObject : EmptyObject;
|
|
94
|
-
type ExtractComponentExportKeys<TComponents, TKeys> = TComponents extends Record<string, ReactComponent> ? TKeys extends readonly (keyof TComponents)[] ? TKeys : readonly [] : readonly [];
|
|
95
|
-
type ExtractProviderDeps<D extends {
|
|
96
|
-
providers?: DefProviderMap;
|
|
97
|
-
imports?: readonly ReactModuleImport[];
|
|
98
|
-
}> = ExtractProviders<D["providers"]> & ExtractImportsExports<D> & NormalizeGlobalDependencies<GlobalDependencies>;
|
|
99
|
-
type ExtractComponentDeps<D extends {
|
|
100
|
-
providers?: DefProviderMap;
|
|
101
|
-
components?: Record<string, ReactComponent>;
|
|
102
|
-
imports?: readonly ReactModuleImport[];
|
|
103
|
-
}> = ExtractProviderDeps<D> & WithoutDepsComponentMap<ExtractComponents<D["components"]>> & ExtractImportsComponentExports<D>;
|
|
104
|
-
type ExtractImportsExports<D extends {
|
|
105
|
-
imports?: readonly ReactModuleImport[];
|
|
106
|
-
}> = D["imports"] extends readonly ReactModuleImport[] ? ExtractExportsFromImports<D["imports"]> : EmptyObject;
|
|
107
|
-
type ExtractImportsComponentExports<D extends {
|
|
108
|
-
imports?: readonly ReactModuleImport[];
|
|
109
|
-
}> = D["imports"] extends readonly ReactModuleImport[] ? ExtractComponentExportsFromImports<D["imports"]> : EmptyObject;
|
|
110
|
-
type ExtractModuleDefFromModule<T> = T extends {
|
|
111
|
-
[reactImportModuleDefMarker]?: infer Def;
|
|
112
|
-
} ? Def : (T extends {
|
|
113
|
-
exports: infer E;
|
|
114
|
-
} ? {
|
|
115
|
-
exports: E;
|
|
116
|
-
} : EmptyObject) & (T extends {
|
|
117
|
-
componentExports: infer CE;
|
|
118
|
-
} ? {
|
|
119
|
-
componentExports: CE;
|
|
120
|
-
} : EmptyObject);
|
|
121
|
-
type ExtractExportsFromImports<T extends readonly ReactModuleImport[]> = T extends readonly [
|
|
122
|
-
infer First,
|
|
123
|
-
...infer Rest extends readonly ReactModuleImport[]
|
|
124
|
-
] ? ExtractModuleDefFromModule<First> extends {
|
|
125
|
-
exports: infer E;
|
|
126
|
-
} ? E & ExtractExportsFromImports<Rest> : ExtractExportsFromImports<Rest> : EmptyObject;
|
|
127
|
-
type ExtractComponentExportsFromImports<T extends readonly ReactModuleImport[]> = T extends readonly [
|
|
128
|
-
infer First,
|
|
129
|
-
...infer Rest extends readonly ReactModuleImport[]
|
|
130
|
-
] ? ExtractModuleDefFromModule<First> extends {
|
|
131
|
-
componentExports: infer E;
|
|
132
|
-
} ? WithoutDepsComponentMap<E> & ExtractComponentExportsFromImports<Rest> : ExtractComponentExportsFromImports<Rest> : EmptyObject;
|
|
133
|
-
type WithProviders<TProviders extends DefProviderMap | EmptyObject, TDeps extends object> = TProviders extends EmptyObject ? {
|
|
134
|
-
providers?: EmptyObject;
|
|
135
|
-
} : {
|
|
136
|
-
providers: ToReactProviderMap<TProviders, TDeps>;
|
|
137
|
-
};
|
|
138
|
-
type ToReactProviderMap<T extends DefProviderMap, DepsMap extends object = Record<string, unknown>> = [keyof T] extends [never] ? EmptyObject : {
|
|
139
|
-
[K in keyof T]: T[K] extends object ? (Provider<T[K], KnownStringKeyMap<DepsMap>, NonScopedResolverOptions<T>> & {
|
|
140
|
-
initAfter?: readonly Extract<keyof DepsMap, string>[];
|
|
141
|
-
}) | T[K] : T[K];
|
|
142
|
-
};
|
|
143
|
-
export type NonScopedResolverOptions<T> = Omit<BuildResolverOptions<T>, "lifetime"> & {
|
|
144
|
-
lifetime?: Exclude<LifetimeType, "SCOPED">;
|
|
145
|
-
};
|
|
146
|
-
type KnownStringKeyMap<T extends object> = {
|
|
147
|
-
[K in Extract<keyof T, string>]: T[K];
|
|
148
|
-
};
|
|
149
|
-
type WithImports<TImports extends readonly unknown[]> = TImports extends readonly [] ? {
|
|
150
|
-
imports?: readonly [];
|
|
151
|
-
} : {
|
|
152
|
-
imports: TImports;
|
|
153
|
-
};
|
|
154
|
-
type WithExports<TKeys extends readonly string[]> = TKeys extends readonly [] ? {
|
|
155
|
-
exports?: readonly [];
|
|
156
|
-
} : {
|
|
157
|
-
exports: TKeys;
|
|
158
|
-
};
|
|
159
|
-
type WithComponents<TComponents extends Record<string, ReactComponent> | EmptyObject> = TComponents extends EmptyObject ? {
|
|
160
|
-
components?: EmptyObject;
|
|
161
|
-
} : {
|
|
162
|
-
components: TComponents;
|
|
163
|
-
};
|
|
164
|
-
type WithComponentExports<TKeys extends readonly string[]> = TKeys extends readonly [] ? {
|
|
165
|
-
componentExports?: readonly [];
|
|
166
|
-
} : {
|
|
167
|
-
componentExports: TKeys;
|
|
168
|
-
};
|
|
169
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|