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
package/package.json
CHANGED
|
@@ -3,9 +3,6 @@
|
|
|
3
3
|
"description": "Transport-agnostic modular DI and CQRS framework for Awilix with Nest-like module patterns and strict type safety",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
|
-
"bin": {
|
|
7
|
-
"awilixify": "./dist/lib/codegen/cli.js"
|
|
8
|
-
},
|
|
9
6
|
"exports": {
|
|
10
7
|
".": {
|
|
11
8
|
"types": "./dist/index.d.ts",
|
|
@@ -15,20 +12,16 @@
|
|
|
15
12
|
"types": "./dist/lib/http/index.d.ts",
|
|
16
13
|
"import": "./dist/lib/http/index.js"
|
|
17
14
|
},
|
|
18
|
-
"./
|
|
19
|
-
"types": "./dist/lib/
|
|
20
|
-
"import": "./dist/lib/
|
|
15
|
+
"./extension-api": {
|
|
16
|
+
"types": "./dist/lib/extension-api/index.d.ts",
|
|
17
|
+
"import": "./dist/lib/extension-api/index.js"
|
|
21
18
|
},
|
|
22
19
|
"./devtools": {
|
|
23
20
|
"types": "./dist/lib/devtools/index.d.ts",
|
|
24
21
|
"import": "./dist/lib/devtools/index.js"
|
|
25
|
-
},
|
|
26
|
-
"./react": {
|
|
27
|
-
"types": "./dist/lib/react/index.d.ts",
|
|
28
|
-
"import": "./dist/lib/react/index.js"
|
|
29
22
|
}
|
|
30
23
|
},
|
|
31
|
-
"version": "3.1
|
|
24
|
+
"version": "3.2.1",
|
|
32
25
|
"license": "MIT",
|
|
33
26
|
"scripts": {
|
|
34
27
|
"build": "tsc",
|
|
@@ -47,30 +40,14 @@
|
|
|
47
40
|
"test:types:watch": "tstyche --watch"
|
|
48
41
|
},
|
|
49
42
|
"peerDependencies": {
|
|
50
|
-
"
|
|
51
|
-
"awilix": ">=9.0.0",
|
|
52
|
-
"react": ">=18.0.0 || >=19.0.0"
|
|
53
|
-
},
|
|
54
|
-
"peerDependenciesMeta": {
|
|
55
|
-
"@asyncapi/modelina": {
|
|
56
|
-
"optional": true
|
|
57
|
-
},
|
|
58
|
-
"react": {
|
|
59
|
-
"optional": true
|
|
60
|
-
}
|
|
43
|
+
"awilix": ">=9.0.0"
|
|
61
44
|
},
|
|
62
45
|
"devDependencies": {
|
|
63
|
-
"@asyncapi/modelina": "^5.10.1",
|
|
64
46
|
"@biomejs/biome": "2.3.15",
|
|
65
47
|
"@shikijs/vitepress-twoslash": "^4.0.2",
|
|
66
|
-
"@testing-library/react": "^16.3.0",
|
|
67
48
|
"@types/node": "^22.15.21",
|
|
68
|
-
"@types/react": "^19.2.7",
|
|
69
49
|
"@vitest/coverage-v8": "^4.0.18",
|
|
70
50
|
"del-cli": "^7.0.0",
|
|
71
|
-
"happy-dom": "^20.0.11",
|
|
72
|
-
"react": "^19.2.3",
|
|
73
|
-
"react-dom": "^19.2.3",
|
|
74
51
|
"tstyche": "^5.0.2",
|
|
75
52
|
"typescript": "^5.9.3",
|
|
76
53
|
"vite": "^6.4.2",
|
package/dist/lib/codegen/cli.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { dirname, resolve } from "node:path";
|
|
3
|
-
import { pathToFileURL } from "node:url";
|
|
4
|
-
import { generateAsyncApiClient, generateAsyncApiConsumer, } from "./generate-asyncapi-client.js";
|
|
5
|
-
import { generateOrvalApiClient } from "./generate-orval-api-client.js";
|
|
6
|
-
const [, , command, ...args] = process.argv;
|
|
7
|
-
if (command !== "codegen") {
|
|
8
|
-
throw new Error("Usage: awilixify codegen --config <path-to-codegen-config.mjs>");
|
|
9
|
-
}
|
|
10
|
-
const configArgumentIndex = args.indexOf("--config");
|
|
11
|
-
const configPath = args[configArgumentIndex + 1];
|
|
12
|
-
if (configArgumentIndex === -1 || !configPath) {
|
|
13
|
-
throw new Error("The codegen command requires --config");
|
|
14
|
-
}
|
|
15
|
-
const resolvedConfigPath = resolve(configPath);
|
|
16
|
-
const configDirectory = dirname(resolvedConfigPath);
|
|
17
|
-
const configModule = (await import(pathToFileURL(resolvedConfigPath).href));
|
|
18
|
-
const config = configModule.default;
|
|
19
|
-
if (!config) {
|
|
20
|
-
throw new Error("Codegen config must default-export a configuration object");
|
|
21
|
-
}
|
|
22
|
-
for (const client of config.clients ?? []) {
|
|
23
|
-
await generateOrvalApiClient({
|
|
24
|
-
...client,
|
|
25
|
-
outputDirectory: resolve(configDirectory, client.outputDirectory),
|
|
26
|
-
module: client.module
|
|
27
|
-
? {
|
|
28
|
-
...client.module,
|
|
29
|
-
outputPath: resolve(configDirectory, client.module.outputPath),
|
|
30
|
-
}
|
|
31
|
-
: undefined,
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
for (const client of config.asyncApiClients ?? []) {
|
|
35
|
-
await generateAsyncApiClient({
|
|
36
|
-
...client,
|
|
37
|
-
outputDirectory: resolve(configDirectory, client.outputDirectory),
|
|
38
|
-
module: client.module
|
|
39
|
-
? {
|
|
40
|
-
...client.module,
|
|
41
|
-
outputPath: resolve(configDirectory, client.module.outputPath),
|
|
42
|
-
}
|
|
43
|
-
: undefined,
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
for (const consumer of config.asyncApiConsumers ?? []) {
|
|
47
|
-
await generateAsyncApiConsumer({
|
|
48
|
-
...consumer,
|
|
49
|
-
outputDirectory: resolve(configDirectory, consumer.outputDirectory),
|
|
50
|
-
module: consumer.module
|
|
51
|
-
? {
|
|
52
|
-
...consumer.module,
|
|
53
|
-
outputPath: resolve(configDirectory, consumer.module.outputPath),
|
|
54
|
-
}
|
|
55
|
-
: undefined,
|
|
56
|
-
});
|
|
57
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { GenerateAsyncApiClientOptions, GenerateAsyncApiConsumerOptions } from "./generate-asyncapi-client.js";
|
|
2
|
-
import type { GenerateOrvalApiClientOptions } from "./generate-orval-api-client.js";
|
|
3
|
-
export type AwilixifyCodegenConfig = {
|
|
4
|
-
asyncApiClients?: GenerateAsyncApiClientOptions[];
|
|
5
|
-
asyncApiConsumers?: GenerateAsyncApiConsumerOptions[];
|
|
6
|
-
clients?: GenerateOrvalApiClientOptions[];
|
|
7
|
-
};
|
|
8
|
-
export declare function defineCodegenConfig(config: AwilixifyCodegenConfig): AwilixifyCodegenConfig;
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
type JsonObject = Record<string, unknown>;
|
|
2
|
-
type AsyncApiDocument = {
|
|
3
|
-
channels?: Record<string, JsonObject>;
|
|
4
|
-
operations?: Record<string, JsonObject>;
|
|
5
|
-
};
|
|
6
|
-
export type GenerateAsyncApiClientModuleOptions = {
|
|
7
|
-
clientImport: string;
|
|
8
|
-
clientProviderName: string;
|
|
9
|
-
messagesImport: string;
|
|
10
|
-
moduleName: string;
|
|
11
|
-
outputPath: string;
|
|
12
|
-
};
|
|
13
|
-
export type GenerateAsyncApiClientOptions = {
|
|
14
|
-
asyncApi: string | AsyncApiDocument;
|
|
15
|
-
clientClassName: string;
|
|
16
|
-
messagesConstName: string;
|
|
17
|
-
outputDirectory: string;
|
|
18
|
-
runtimeImport: string;
|
|
19
|
-
serviceName: string;
|
|
20
|
-
include?: readonly string[];
|
|
21
|
-
clientFileName?: string;
|
|
22
|
-
messagesFileName?: string;
|
|
23
|
-
module?: GenerateAsyncApiClientModuleOptions;
|
|
24
|
-
};
|
|
25
|
-
export type GenerateAsyncApiClientResult = {
|
|
26
|
-
clientPath: string;
|
|
27
|
-
messagesPath: string;
|
|
28
|
-
moduleCreated?: boolean;
|
|
29
|
-
modulePath?: string;
|
|
30
|
-
operationIds: string[];
|
|
31
|
-
};
|
|
32
|
-
export type GenerateAsyncApiConsumerModuleOptions = {
|
|
33
|
-
messagesImport: string;
|
|
34
|
-
moduleName: string;
|
|
35
|
-
outputPath: string;
|
|
36
|
-
};
|
|
37
|
-
export type GenerateAsyncApiConsumerOptions = {
|
|
38
|
-
asyncApi: string | AsyncApiDocument;
|
|
39
|
-
messagesConstName: string;
|
|
40
|
-
outputDirectory: string;
|
|
41
|
-
runtimeImport: string;
|
|
42
|
-
serviceName: string;
|
|
43
|
-
include?: readonly string[];
|
|
44
|
-
messagesFileName?: string;
|
|
45
|
-
module?: GenerateAsyncApiConsumerModuleOptions;
|
|
46
|
-
};
|
|
47
|
-
export type GenerateAsyncApiConsumerResult = {
|
|
48
|
-
messagesPath: string;
|
|
49
|
-
moduleCreated?: boolean;
|
|
50
|
-
modulePath?: string;
|
|
51
|
-
operationIds: string[];
|
|
52
|
-
};
|
|
53
|
-
export declare function generateAsyncApiClient(options: GenerateAsyncApiClientOptions): Promise<GenerateAsyncApiClientResult>;
|
|
54
|
-
export declare function generateAsyncApiConsumer(options: GenerateAsyncApiConsumerOptions): Promise<GenerateAsyncApiConsumerResult>;
|
|
55
|
-
export {};
|
|
@@ -1,359 +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
|
-
export async function generateAsyncApiClient(options) {
|
|
5
|
-
assertIdentifier(options.clientClassName, "client class name");
|
|
6
|
-
assertIdentifier(options.messagesConstName, "messages constant name");
|
|
7
|
-
const document = await resolveDocument(options.asyncApi);
|
|
8
|
-
const operations = collectOperations(document, options.include, "receive");
|
|
9
|
-
const outputDirectory = resolve(options.outputDirectory);
|
|
10
|
-
const clientFileName = options.clientFileName ?? "asyncapi-client.ts";
|
|
11
|
-
const messagesFileName = options.messagesFileName ?? "asyncapi.messages.ts";
|
|
12
|
-
const clientPath = resolve(outputDirectory, clientFileName);
|
|
13
|
-
const messagesPath = resolve(outputDirectory, messagesFileName);
|
|
14
|
-
const modulePath = options.module
|
|
15
|
-
? resolve(options.module.outputPath)
|
|
16
|
-
: undefined;
|
|
17
|
-
await mkdir(outputDirectory, { recursive: true });
|
|
18
|
-
const messagesSource = await renderMessages(options, operations, document);
|
|
19
|
-
await Promise.all([
|
|
20
|
-
replaceFile(clientPath, renderClient(options, operations, toJavaScriptImport(messagesFileName))),
|
|
21
|
-
replaceFile(messagesPath, messagesSource),
|
|
22
|
-
]);
|
|
23
|
-
const moduleCreated = modulePath && options.module
|
|
24
|
-
? await createFileIfMissing(modulePath, renderModule(options, options.module))
|
|
25
|
-
: undefined;
|
|
26
|
-
return {
|
|
27
|
-
clientPath,
|
|
28
|
-
messagesPath,
|
|
29
|
-
moduleCreated,
|
|
30
|
-
modulePath,
|
|
31
|
-
operationIds: operations.map((operation) => operation.operationId),
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
export async function generateAsyncApiConsumer(options) {
|
|
35
|
-
assertIdentifier(options.messagesConstName, "messages constant name");
|
|
36
|
-
const document = await resolveDocument(options.asyncApi);
|
|
37
|
-
const operations = collectOperations(document, options.include, "send");
|
|
38
|
-
const outputDirectory = resolve(options.outputDirectory);
|
|
39
|
-
const messagesFileName = options.messagesFileName ?? "asyncapi.events.ts";
|
|
40
|
-
const messagesPath = resolve(outputDirectory, messagesFileName);
|
|
41
|
-
const modulePath = options.module
|
|
42
|
-
? resolve(options.module.outputPath)
|
|
43
|
-
: undefined;
|
|
44
|
-
await mkdir(outputDirectory, { recursive: true });
|
|
45
|
-
await replaceFile(messagesPath, await renderMessages(options, operations, document));
|
|
46
|
-
const moduleCreated = modulePath && options.module
|
|
47
|
-
? await createFileIfMissing(modulePath, renderConsumerModule(options, options.module))
|
|
48
|
-
: undefined;
|
|
49
|
-
return {
|
|
50
|
-
messagesPath,
|
|
51
|
-
moduleCreated,
|
|
52
|
-
modulePath,
|
|
53
|
-
operationIds: operations.map((operation) => operation.operationId),
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
async function resolveDocument(input) {
|
|
57
|
-
if (typeof input !== "string")
|
|
58
|
-
return input;
|
|
59
|
-
const response = await fetch(input);
|
|
60
|
-
if (!response.ok) {
|
|
61
|
-
throw new Error(`Failed to fetch AsyncAPI document from ${input}: ${response.status} ${response.statusText}`);
|
|
62
|
-
}
|
|
63
|
-
return (await response.json());
|
|
64
|
-
}
|
|
65
|
-
function collectOperations(document, include, action) {
|
|
66
|
-
const selected = new Set(include);
|
|
67
|
-
const operations = [];
|
|
68
|
-
for (const [operationId, operation] of Object.entries(document.operations ?? {})) {
|
|
69
|
-
if (operation.action !== action)
|
|
70
|
-
continue;
|
|
71
|
-
if (selected.size && !selected.has(operationId))
|
|
72
|
-
continue;
|
|
73
|
-
assertIdentifier(operationId, "AsyncAPI operation ID");
|
|
74
|
-
const channel = resolveReference(document, operation.channel, "channel");
|
|
75
|
-
const messages = Array.isArray(operation.messages)
|
|
76
|
-
? operation.messages
|
|
77
|
-
: [];
|
|
78
|
-
const message = resolveReference(document, messages[0], "message");
|
|
79
|
-
const address = requireString(channel.address, `${operationId} channel address`);
|
|
80
|
-
const payload = message.payload === undefined
|
|
81
|
-
? undefined
|
|
82
|
-
: requireObject(message.payload, `${operationId} message payload`);
|
|
83
|
-
const bindings = requireObject(channel.bindings, `${operationId} channel bindings`);
|
|
84
|
-
const amqp = requireObject(bindings.amqp, `${operationId} AMQP binding`);
|
|
85
|
-
const exchangeValue = requireObject(amqp.exchange, `${operationId} AMQP exchange`);
|
|
86
|
-
operations.push({
|
|
87
|
-
address,
|
|
88
|
-
exchange: {
|
|
89
|
-
autoDelete: optionalBoolean(exchangeValue.autoDelete),
|
|
90
|
-
durable: optionalBoolean(exchangeValue.durable),
|
|
91
|
-
name: requireString(exchangeValue.name, `${operationId} exchange name`),
|
|
92
|
-
type: typeof exchangeValue.type === "string"
|
|
93
|
-
? exchangeValue.type
|
|
94
|
-
: undefined,
|
|
95
|
-
},
|
|
96
|
-
messageType: typeof message["x-awilixify-type"] === "string"
|
|
97
|
-
? message["x-awilixify-type"]
|
|
98
|
-
: requireString(message.name, `${operationId} message type`),
|
|
99
|
-
operationId,
|
|
100
|
-
payload,
|
|
101
|
-
payloadTypeName: payload
|
|
102
|
-
? `${toPascalCase(operationId)}Payload`
|
|
103
|
-
: undefined,
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
if (selected.size) {
|
|
107
|
-
const found = new Set(operations.map((operation) => operation.operationId));
|
|
108
|
-
const missing = [...selected].filter((operationId) => !found.has(operationId));
|
|
109
|
-
if (missing.length) {
|
|
110
|
-
throw new Error(`AsyncAPI ${action} operations not found: ${missing.join(", ")}`);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
if (!operations.length) {
|
|
114
|
-
throw new Error(`No AsyncAPI ${action} operations selected for generation`);
|
|
115
|
-
}
|
|
116
|
-
return operations;
|
|
117
|
-
}
|
|
118
|
-
function resolveReference(document, value, label) {
|
|
119
|
-
const object = requireObject(value, label);
|
|
120
|
-
if (typeof object.$ref !== "string")
|
|
121
|
-
return object;
|
|
122
|
-
if (!object.$ref.startsWith("#/")) {
|
|
123
|
-
throw new Error(`Only local AsyncAPI references are supported: ${object.$ref}`);
|
|
124
|
-
}
|
|
125
|
-
let current = document;
|
|
126
|
-
for (const encodedPart of object.$ref.slice(2).split("/")) {
|
|
127
|
-
const part = encodedPart.replaceAll("~1", "/").replaceAll("~0", "~");
|
|
128
|
-
current = requireObject(current, object.$ref)[part];
|
|
129
|
-
}
|
|
130
|
-
return requireObject(current, object.$ref);
|
|
131
|
-
}
|
|
132
|
-
async function renderMessages(options, operations, document) {
|
|
133
|
-
const payloadModels = await renderPayloadModels(operations, document);
|
|
134
|
-
const entries = operations
|
|
135
|
-
.map((operation) => `\t${operation.operationId}: defineRabbitMessage${operation.payloadTypeName ? `<${operation.payloadTypeName}>` : ""}({
|
|
136
|
-
\t\texchange: ${indent(renderExchange(operation.exchange), 2)},
|
|
137
|
-
\t\troutingKey: ${JSON.stringify(operation.address)},
|
|
138
|
-
\t\tserviceName: ${JSON.stringify(options.serviceName)},
|
|
139
|
-
\t\ttype: ${JSON.stringify(operation.messageType)},
|
|
140
|
-
\t}),`)
|
|
141
|
-
.join("\n");
|
|
142
|
-
return `// Generated from AsyncAPI. Do not edit.
|
|
143
|
-
import { defineRabbitMessage } from ${JSON.stringify(options.runtimeImport)};
|
|
144
|
-
|
|
145
|
-
${payloadModels}
|
|
146
|
-
|
|
147
|
-
export const ${options.messagesConstName} = {
|
|
148
|
-
${entries}
|
|
149
|
-
} as const;
|
|
150
|
-
`;
|
|
151
|
-
}
|
|
152
|
-
async function renderPayloadModels(operations, document) {
|
|
153
|
-
if (!operations.some((operation) => operation.payload))
|
|
154
|
-
return "";
|
|
155
|
-
let TypeScriptGenerator;
|
|
156
|
-
try {
|
|
157
|
-
({ TypeScriptGenerator } = await import("@asyncapi/modelina"));
|
|
158
|
-
}
|
|
159
|
-
catch (error) {
|
|
160
|
-
throw new Error('AsyncAPI codegen requires the optional peer dependency "@asyncapi/modelina"', { cause: error });
|
|
161
|
-
}
|
|
162
|
-
const rendered = [];
|
|
163
|
-
for (const operation of operations) {
|
|
164
|
-
if (!operation.payload || !operation.payloadTypeName)
|
|
165
|
-
continue;
|
|
166
|
-
const operationPrefix = toPascalCase(operation.operationId);
|
|
167
|
-
const generator = new TypeScriptGenerator({
|
|
168
|
-
modelType: "interface",
|
|
169
|
-
processorOptions: {
|
|
170
|
-
jsonSchema: { ignoreAdditionalProperties: true },
|
|
171
|
-
},
|
|
172
|
-
constraints: {
|
|
173
|
-
modelName: ({ modelName }) => {
|
|
174
|
-
const constrainedName = toPascalCase(modelName);
|
|
175
|
-
return constrainedName === operation.payloadTypeName
|
|
176
|
-
? constrainedName
|
|
177
|
-
: `${operationPrefix}${constrainedName}`;
|
|
178
|
-
},
|
|
179
|
-
},
|
|
180
|
-
});
|
|
181
|
-
const models = await generator.generate({
|
|
182
|
-
...requireObject(resolveSchemaReferences(operation.payload, document), `${operation.operationId} resolved payload`),
|
|
183
|
-
$id: operation.payloadTypeName,
|
|
184
|
-
});
|
|
185
|
-
rendered.push(...models.map((model) => `export ${model.result}`));
|
|
186
|
-
}
|
|
187
|
-
return rendered.join("\n\n");
|
|
188
|
-
}
|
|
189
|
-
function renderClient(options, operations, messagesImport) {
|
|
190
|
-
const methods = operations
|
|
191
|
-
.map((operation) => operation.payloadTypeName
|
|
192
|
-
? `\t@callsOperation(${options.messagesConstName}.${operation.operationId})
|
|
193
|
-
\t${operation.operationId}(
|
|
194
|
-
\t\tpayload: MessagePayload<typeof ${options.messagesConstName}.${operation.operationId}>,
|
|
195
|
-
\t): Promise<void> {
|
|
196
|
-
\t\treturn this.rabbitPublisher.publish({
|
|
197
|
-
\t\t\tmessage: this.rabbitPublisher.messages.${operation.operationId},
|
|
198
|
-
\t\t\tpayload,
|
|
199
|
-
\t\t});
|
|
200
|
-
\t}`
|
|
201
|
-
: `\t@callsOperation(${options.messagesConstName}.${operation.operationId})
|
|
202
|
-
\t${operation.operationId}(): Promise<void> {
|
|
203
|
-
\t\treturn this.rabbitPublisher.publish({
|
|
204
|
-
\t\t\tmessage: this.rabbitPublisher.messages.${operation.operationId},
|
|
205
|
-
\t\t});
|
|
206
|
-
\t}`)
|
|
207
|
-
.join("\n\n");
|
|
208
|
-
return `// Generated from AsyncAPI. Do not edit.
|
|
209
|
-
|
|
210
|
-
import type { MessagePayload, RabbitPublisher } from ${JSON.stringify(options.runtimeImport)};
|
|
211
|
-
import { callsOperation } from "awilixify";
|
|
212
|
-
|
|
213
|
-
import { ${options.messagesConstName} } from ${JSON.stringify(messagesImport)};
|
|
214
|
-
|
|
215
|
-
export class ${options.clientClassName} {
|
|
216
|
-
\tconstructor(
|
|
217
|
-
\t\tprivate readonly rabbitPublisher: RabbitPublisher<typeof ${options.messagesConstName}>,
|
|
218
|
-
\t) {}
|
|
219
|
-
|
|
220
|
-
${methods}
|
|
221
|
-
}
|
|
222
|
-
`;
|
|
223
|
-
}
|
|
224
|
-
function renderModule(options, module) {
|
|
225
|
-
assertIdentifier(module.moduleName, "module name");
|
|
226
|
-
assertIdentifier(module.clientProviderName, "client provider name");
|
|
227
|
-
const moduleDefName = `${module.moduleName}Def`;
|
|
228
|
-
const rabbitMqModuleName = `${module.moduleName.replace(/Module$/, "")}RabbitMqModule`;
|
|
229
|
-
assertIdentifier(rabbitMqModuleName, "RabbitMQ module name");
|
|
230
|
-
return `// Generated once by Awilixify codegen. Safe to customize.
|
|
231
|
-
import { RabbitMqModule } from ${JSON.stringify(options.runtimeImport)};
|
|
232
|
-
import { createModule, type ModuleDef } from "awilixify";
|
|
233
|
-
|
|
234
|
-
import { ${options.clientClassName} } from ${JSON.stringify(module.clientImport)};
|
|
235
|
-
import { ${options.messagesConstName} } from ${JSON.stringify(module.messagesImport)};
|
|
236
|
-
|
|
237
|
-
const ${rabbitMqModuleName} = RabbitMqModule({
|
|
238
|
-
\tpublisher: ${options.messagesConstName},
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
export type ${moduleDefName} = ModuleDef<{
|
|
242
|
-
\texportKeys: [${JSON.stringify(module.clientProviderName)}];
|
|
243
|
-
\timports: [typeof ${rabbitMqModuleName}];
|
|
244
|
-
\tproviders: {
|
|
245
|
-
\t\t${module.clientProviderName}: ${options.clientClassName};
|
|
246
|
-
\t};
|
|
247
|
-
}>;
|
|
248
|
-
|
|
249
|
-
export type Deps = ${moduleDefName}["deps"];
|
|
250
|
-
|
|
251
|
-
export const ${module.moduleName} = createModule<${moduleDefName}>({
|
|
252
|
-
\tname: ${JSON.stringify(module.moduleName)},
|
|
253
|
-
\timports: [${rabbitMqModuleName}],
|
|
254
|
-
\tproviders: {
|
|
255
|
-
\t\t${module.clientProviderName}: ${options.clientClassName},
|
|
256
|
-
\t},
|
|
257
|
-
\texports: [${JSON.stringify(module.clientProviderName)}],
|
|
258
|
-
});
|
|
259
|
-
`;
|
|
260
|
-
}
|
|
261
|
-
function renderConsumerModule(options, module) {
|
|
262
|
-
assertIdentifier(module.moduleName, "module name");
|
|
263
|
-
return `// Generated once by Awilixify codegen. Safe to customize.
|
|
264
|
-
import { RabbitMqModule } from ${JSON.stringify(options.runtimeImport)};
|
|
265
|
-
|
|
266
|
-
import { ${options.messagesConstName} } from ${JSON.stringify(module.messagesImport)};
|
|
267
|
-
|
|
268
|
-
export const ${module.moduleName} = RabbitMqModule({
|
|
269
|
-
consumer: Object.values(${options.messagesConstName}),
|
|
270
|
-
});
|
|
271
|
-
`;
|
|
272
|
-
}
|
|
273
|
-
function renderExchange(exchange) {
|
|
274
|
-
return JSON.stringify({
|
|
275
|
-
name: exchange.name,
|
|
276
|
-
options: {
|
|
277
|
-
autoDelete: exchange.autoDelete ?? false,
|
|
278
|
-
durable: exchange.durable ?? true,
|
|
279
|
-
},
|
|
280
|
-
type: exchange.type ?? "topic",
|
|
281
|
-
}, null, "\t");
|
|
282
|
-
}
|
|
283
|
-
function indent(value, depth) {
|
|
284
|
-
const prefix = "\t".repeat(depth);
|
|
285
|
-
return value.replaceAll("\n", `\n${prefix}`);
|
|
286
|
-
}
|
|
287
|
-
function resolveSchemaReferences(value, document) {
|
|
288
|
-
if (Array.isArray(value)) {
|
|
289
|
-
return value.map((item) => resolveSchemaReferences(item, document));
|
|
290
|
-
}
|
|
291
|
-
if (!value || typeof value !== "object")
|
|
292
|
-
return value;
|
|
293
|
-
const object = value;
|
|
294
|
-
if (typeof object.$ref === "string") {
|
|
295
|
-
return resolveSchemaReferences(resolveReference(document, object, "schema"), document);
|
|
296
|
-
}
|
|
297
|
-
return Object.fromEntries(Object.entries(object).map(([key, item]) => [
|
|
298
|
-
key,
|
|
299
|
-
resolveSchemaReferences(item, document),
|
|
300
|
-
]));
|
|
301
|
-
}
|
|
302
|
-
async function replaceFile(filePath, source) {
|
|
303
|
-
const temporaryPath = `${filePath}.${randomUUID()}.tmp`;
|
|
304
|
-
try {
|
|
305
|
-
await writeFile(temporaryPath, source);
|
|
306
|
-
await rename(temporaryPath, filePath);
|
|
307
|
-
}
|
|
308
|
-
finally {
|
|
309
|
-
await rm(temporaryPath, { force: true });
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
async function createFileIfMissing(filePath, source) {
|
|
313
|
-
await mkdir(dirname(filePath), { recursive: true });
|
|
314
|
-
try {
|
|
315
|
-
await writeFile(filePath, source, { flag: "wx" });
|
|
316
|
-
return true;
|
|
317
|
-
}
|
|
318
|
-
catch (error) {
|
|
319
|
-
if (typeof error === "object" &&
|
|
320
|
-
error !== null &&
|
|
321
|
-
"code" in error &&
|
|
322
|
-
error.code === "EEXIST") {
|
|
323
|
-
return false;
|
|
324
|
-
}
|
|
325
|
-
throw error;
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
function requireObject(value, label) {
|
|
329
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
330
|
-
throw new Error(`${label} must be an object`);
|
|
331
|
-
}
|
|
332
|
-
return value;
|
|
333
|
-
}
|
|
334
|
-
function requireString(value, label) {
|
|
335
|
-
if (typeof value !== "string" || !value) {
|
|
336
|
-
throw new Error(`${label} must be a non-empty string`);
|
|
337
|
-
}
|
|
338
|
-
return value;
|
|
339
|
-
}
|
|
340
|
-
function optionalBoolean(value) {
|
|
341
|
-
return typeof value === "boolean" ? value : undefined;
|
|
342
|
-
}
|
|
343
|
-
function assertIdentifier(value, label) {
|
|
344
|
-
if (!/^[$A-Z_a-z][$\w]*$/.test(value)) {
|
|
345
|
-
throw new Error(`${label} "${value}" is not a valid TypeScript identifier`);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
function toPascalCase(value) {
|
|
349
|
-
const result = value
|
|
350
|
-
.replaceAll(/([a-z\d])([A-Z])/g, "$1 $2")
|
|
351
|
-
.split(/[^A-Za-z\d]+/)
|
|
352
|
-
.filter(Boolean)
|
|
353
|
-
.map((part) => `${part[0]?.toUpperCase()}${part.slice(1)}`)
|
|
354
|
-
.join("");
|
|
355
|
-
return result || "Payload";
|
|
356
|
-
}
|
|
357
|
-
function toJavaScriptImport(fileName) {
|
|
358
|
-
return `./${basename(fileName, extname(fileName))}.js`;
|
|
359
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
type OpenApiOperation = {
|
|
2
|
-
operationId?: unknown;
|
|
3
|
-
tags?: unknown;
|
|
4
|
-
};
|
|
5
|
-
type OpenApiDocument = {
|
|
6
|
-
paths?: Record<string, Record<string, OpenApiOperation>>;
|
|
7
|
-
};
|
|
8
|
-
export type GenerateApiClientModuleOptions = {
|
|
9
|
-
outputPath: string;
|
|
10
|
-
moduleName: string;
|
|
11
|
-
apiClientProviderName: string;
|
|
12
|
-
apiClientImport: string;
|
|
13
|
-
exports?: readonly string[];
|
|
14
|
-
};
|
|
15
|
-
export type GenerateOrvalApiClientOptions = {
|
|
16
|
-
serviceName: string;
|
|
17
|
-
openApi: string | OpenApiDocument;
|
|
18
|
-
outputDirectory: string;
|
|
19
|
-
generatedClientImport: string;
|
|
20
|
-
apiClientClassName: string;
|
|
21
|
-
operationsConstName: string;
|
|
22
|
-
tags?: readonly string[];
|
|
23
|
-
apiClientFileName?: string;
|
|
24
|
-
operationsFileName?: string;
|
|
25
|
-
getOperationFunctionName?: (operationId: string) => string;
|
|
26
|
-
module?: GenerateApiClientModuleOptions;
|
|
27
|
-
};
|
|
28
|
-
export type GenerateOrvalApiClientResult = {
|
|
29
|
-
operationIds: string[];
|
|
30
|
-
apiClientPath: string;
|
|
31
|
-
operationsPath: string;
|
|
32
|
-
modulePath?: string;
|
|
33
|
-
moduleCreated?: boolean;
|
|
34
|
-
};
|
|
35
|
-
export declare function generateOrvalApiClient(options: GenerateOrvalApiClientOptions): Promise<GenerateOrvalApiClientResult>;
|
|
36
|
-
export {};
|