polyapi 0.23.2 → 0.23.4
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/build/client/src/commands/{generate.d.ts → generate/index.d.ts} +2 -2
- package/build/client/src/commands/generate/index.d.ts.map +1 -0
- package/build/client/src/commands/generate/index.js +321 -0
- package/build/client/src/commands/generate/index.js.map +1 -0
- package/build/client/src/commands/generate/schemaTypes.d.ts +54 -0
- package/build/client/src/commands/generate/schemaTypes.d.ts.map +1 -0
- package/build/client/src/commands/generate/schemaTypes.js +440 -0
- package/build/client/src/commands/generate/schemaTypes.js.map +1 -0
- package/build/client/src/commands/generate/types.d.ts +6 -0
- package/build/client/src/commands/generate/types.d.ts.map +1 -0
- package/build/client/src/commands/{generate.js → generate/types.js} +180 -509
- package/build/client/src/commands/generate/types.js.map +1 -0
- package/build/client/src/templates/index.d.ts.hbs +1 -0
- package/build/client/src/templates/poly-custom.js.hbs +1 -0
- package/build/client/src/utils.d.ts +8 -0
- package/build/client/src/utils.d.ts.map +1 -1
- package/build/client/src/utils.js +47 -7
- package/build/client/src/utils.js.map +1 -1
- package/build/model/src/dto/function/create-api-function.dto.d.ts.map +1 -1
- package/build/model/src/dto/function/create-api-function.dto.js +2 -1
- package/build/model/src/dto/function/create-api-function.dto.js.map +1 -1
- package/build/model/src/dto/function/create-custom-function.dto.d.ts +1 -0
- package/build/model/src/dto/function/create-custom-function.dto.d.ts.map +1 -1
- package/build/model/src/dto/function/create-custom-function.dto.js +6 -1
- package/build/model/src/dto/function/create-custom-function.dto.js.map +1 -1
- package/build/model/src/dto/function/function.dto.d.ts +2 -0
- package/build/model/src/dto/function/function.dto.d.ts.map +1 -1
- package/build/model/src/dto/function/function.dto.js.map +1 -1
- package/build/model/src/dto/function/replicate-api-functions-response.dto.d.ts +8 -2
- package/build/model/src/dto/function/replicate-api-functions-response.dto.d.ts.map +1 -1
- package/build/model/src/dto/function/replicate-api-functions-response.dto.js +29 -8
- package/build/model/src/dto/function/replicate-api-functions-response.dto.js.map +1 -1
- package/build/model/src/dto/function/replicate-api-functions.dto.d.ts +1 -0
- package/build/model/src/dto/function/replicate-api-functions.dto.d.ts.map +1 -1
- package/build/model/src/dto/function/replicate-api-functions.dto.js +5 -0
- package/build/model/src/dto/function/replicate-api-functions.dto.js.map +1 -1
- package/build/model/src/dto/function/update-api-function.dto.d.ts.map +1 -1
- package/build/model/src/dto/function/update-api-function.dto.js +1 -0
- package/build/model/src/dto/function/update-api-function.dto.js.map +1 -1
- package/build/model/src/dto/function/update-custom-function.dto.d.ts +1 -0
- package/build/model/src/dto/function/update-custom-function.dto.d.ts.map +1 -1
- package/build/model/src/dto/function/update-custom-function.dto.js +6 -0
- package/build/model/src/dto/function/update-custom-function.dto.js.map +1 -1
- package/build/model/src/dto/schema/create-schema.dto.js +1 -1
- package/build/model/src/dto/schema/create-schema.dto.js.map +1 -1
- package/build/model/src/dto/schema/schema.dto.d.ts +13 -1
- package/build/model/src/dto/schema/schema.dto.d.ts.map +1 -1
- package/build/model/src/dto/schema/schema.dto.js +8 -1
- package/build/model/src/dto/schema/schema.dto.js.map +1 -1
- package/build/model/src/dto/schema/update-schema.dto.js +3 -3
- package/build/model/src/dto/schema/update-schema.dto.js.map +1 -1
- package/build/model/src/dto/snippet/create-snippet.dto.d.ts.map +1 -1
- package/build/model/src/dto/snippet/create-snippet.dto.js +2 -0
- package/build/model/src/dto/snippet/create-snippet.dto.js.map +1 -1
- package/build/model/src/dto/snippet/update-snippet.dto.d.ts.map +1 -1
- package/build/model/src/dto/snippet/update-snippet.dto.js +3 -1
- package/build/model/src/dto/snippet/update-snippet.dto.js.map +1 -1
- package/build/model/src/dto/validators/context-identifier.js +2 -2
- package/build/model/src/dto/validators/context-identifier.js.map +1 -1
- package/build/model/src/dto/webhook/create-webhook-handle.dto.d.ts.map +1 -1
- package/build/model/src/dto/webhook/create-webhook-handle.dto.js +3 -2
- package/build/model/src/dto/webhook/create-webhook-handle.dto.js.map +1 -1
- package/build/model/src/dto/webhook/update-webhook-handle.dto.d.ts.map +1 -1
- package/build/model/src/dto/webhook/update-webhook-handle.dto.js +1 -0
- package/build/model/src/dto/webhook/update-webhook-handle.dto.js.map +1 -1
- package/build/model/src/specs.d.ts +1 -0
- package/build/model/src/specs.d.ts.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +45 -2
- package/build/client/src/commands/generate.d.ts.map +0 -1
- package/build/client/src/commands/generate.js.map +0 -1
- package/build/client/src/templates/schemas/index.d.ts.hbs +0 -3
- package/build/client/src/templates/schemas/{{context}}.d.ts.hbs +0 -25
|
@@ -26,305 +26,30 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.
|
|
29
|
+
exports.generateVariablesTSDeclarationFile = exports.generateFunctionsTSDeclarationFile = exports.getGenerationErrors = exports.setGenerationErrors = void 0;
|
|
30
30
|
const fs_1 = __importDefault(require("fs"));
|
|
31
31
|
const handlebars_1 = __importDefault(require("handlebars"));
|
|
32
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
33
|
-
const shelljs_1 = __importDefault(require("shelljs"));
|
|
34
32
|
const helper_string_1 = require("@guanghechen/helper-string");
|
|
35
|
-
const prettier_1 = __importDefault(require("prettier"));
|
|
36
33
|
const json_schema_to_typescript_1 = require("json-schema-to-typescript");
|
|
37
34
|
const ts = __importStar(require("typescript"));
|
|
38
|
-
const
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
const api_1 = require("../api");
|
|
43
|
-
const config_1 = require("../config");
|
|
44
|
-
const utils_2 = require("../utils");
|
|
45
|
-
const constants_1 = require("../constants");
|
|
46
|
-
const index_1 = require("../../../common/src/json-schema/index");
|
|
35
|
+
const utils_1 = require("../../../../common/src/utils");
|
|
36
|
+
const specs_1 = require("../../../../common/src/specs");
|
|
37
|
+
const utils_2 = require("../../utils");
|
|
38
|
+
const index_1 = require("../../../../common/src/json-schema/index");
|
|
47
39
|
let generationErrors = false;
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
const prepareDir = async (polyPath) => {
|
|
51
|
-
const libPath = (0, utils_2.getPolyLibPath)(polyPath);
|
|
52
|
-
fs_1.default.rmSync(libPath, { recursive: true, force: true });
|
|
53
|
-
fs_1.default.mkdirSync(libPath, { recursive: true });
|
|
54
|
-
fs_1.default.mkdirSync(`${libPath}/api`);
|
|
55
|
-
fs_1.default.mkdirSync(`${libPath}/client`);
|
|
56
|
-
fs_1.default.mkdirSync(`${libPath}/auth`);
|
|
57
|
-
fs_1.default.mkdirSync(`${libPath}/webhooks`);
|
|
58
|
-
fs_1.default.mkdirSync(`${libPath}/server`);
|
|
59
|
-
fs_1.default.mkdirSync(`${libPath}/vari`);
|
|
60
|
-
fs_1.default.mkdirSync(`${libPath}/schemas`);
|
|
61
|
-
if (polyPath !== constants_1.DEFAULT_POLY_PATH) {
|
|
62
|
-
try {
|
|
63
|
-
await generateRedirectIndexFiles(polyPath);
|
|
64
|
-
}
|
|
65
|
-
catch (err) {
|
|
66
|
-
console.warn(`Could not generate redirect index files: ${err.message}, continuing...`);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
const generateRedirectIndexFiles = async (polyPath) => {
|
|
71
|
-
const defaultPolyLib = (0, utils_2.getPolyLibPath)(constants_1.DEFAULT_POLY_PATH);
|
|
72
|
-
fs_1.default.rmSync(defaultPolyLib, { recursive: true, force: true });
|
|
73
|
-
fs_1.default.mkdirSync(defaultPolyLib, { recursive: true });
|
|
74
|
-
const indexRedirectJSTemplate = handlebars_1.default.compile(await loadTemplate('index-redirect.js.hbs'));
|
|
75
|
-
fs_1.default.writeFileSync(`${defaultPolyLib}/index.js`, indexRedirectJSTemplate({
|
|
76
|
-
polyPath: polyPath.startsWith('/') ? polyPath : `../../../${polyPath}`,
|
|
77
|
-
}));
|
|
78
|
-
const indexTSRedirectJSTemplate = handlebars_1.default.compile(await loadTemplate('index-redirect.d.ts.hbs'));
|
|
79
|
-
fs_1.default.writeFileSync(`${defaultPolyLib}/index.d.ts`, indexTSRedirectJSTemplate({
|
|
80
|
-
polyPath: polyPath.startsWith('/') ? polyPath : `../../../${polyPath}`,
|
|
81
|
-
}));
|
|
82
|
-
};
|
|
83
|
-
const loadTemplate = async (fileName) => fs_1.default.readFileSync(`${__dirname}/../templates/${fileName}`, 'utf8');
|
|
84
|
-
const generateJSFiles = async (libPath, specs) => {
|
|
85
|
-
const apiFunctions = specs.filter((spec) => spec.type === 'apiFunction');
|
|
86
|
-
const customFunctions = specs
|
|
87
|
-
.filter((spec) => spec.type === 'customFunction')
|
|
88
|
-
.filter((spec) => spec.language === 'javascript');
|
|
89
|
-
const webhookHandles = specs.filter((spec) => spec.type === 'webhookHandle');
|
|
90
|
-
const authFunctions = specs.filter((spec) => spec.type === 'authFunction');
|
|
91
|
-
const serverFunctions = specs.filter((spec) => spec.type === 'serverFunction');
|
|
92
|
-
const serverVariables = specs.filter((spec) => spec.type === 'serverVariable');
|
|
93
|
-
await generateIndexJSFile(libPath);
|
|
94
|
-
await generatePolyCustomJSFile(libPath);
|
|
95
|
-
await generateAxiosJSFile(libPath);
|
|
96
|
-
await generateErrorHandlerFile(libPath);
|
|
97
|
-
await generateApiFunctionJSFiles(libPath, apiFunctions);
|
|
98
|
-
const customFnCodeGenerationErrors = await generateCustomFunctionJSFiles(libPath, customFunctions);
|
|
99
|
-
await generateWebhooksJSFiles(libPath, webhookHandles);
|
|
100
|
-
await generateAuthFunctionJSFiles(libPath, authFunctions);
|
|
101
|
-
await generateServerFunctionJSFiles(libPath, serverFunctions);
|
|
102
|
-
await generateServerVariableJSFiles(libPath, serverVariables);
|
|
103
|
-
return customFnCodeGenerationErrors;
|
|
104
|
-
};
|
|
105
|
-
const generateIndexJSFile = async (libPath) => {
|
|
106
|
-
const indexJSTemplate = handlebars_1.default.compile(await loadTemplate('index.js.hbs'));
|
|
107
|
-
fs_1.default.writeFileSync(`${libPath}/index.js`, indexJSTemplate({
|
|
108
|
-
clientID: (0, uuid_1.v4)(),
|
|
109
|
-
apiBaseUrl: getApiBaseUrl(),
|
|
110
|
-
apiKey: getApiKey(),
|
|
111
|
-
}));
|
|
112
|
-
};
|
|
113
|
-
const generatePolyCustomJSFile = async (libPath) => {
|
|
114
|
-
const polyCustomJSTemplate = handlebars_1.default.compile(await loadTemplate('poly-custom.js.hbs'));
|
|
115
|
-
fs_1.default.writeFileSync(`${libPath}/poly-custom.js`, polyCustomJSTemplate({
|
|
116
|
-
apiBaseUrl: getApiBaseUrl(),
|
|
117
|
-
apiKey: getApiKey(),
|
|
118
|
-
}));
|
|
119
|
-
};
|
|
120
|
-
const generateAxiosJSFile = async (libPath) => {
|
|
121
|
-
const axiosJSTemplate = handlebars_1.default.compile(await loadTemplate('axios.js.hbs'));
|
|
122
|
-
fs_1.default.writeFileSync(`${libPath}/axios.js`, axiosJSTemplate({
|
|
123
|
-
apiBaseUrl: getApiBaseUrl(),
|
|
124
|
-
apiKey: getApiKey(),
|
|
125
|
-
}));
|
|
126
|
-
};
|
|
127
|
-
const generateErrorHandlerFile = async (libPath) => {
|
|
128
|
-
const errorHandlerJSTemplate = handlebars_1.default.compile(await loadTemplate('error-handler.js.hbs'));
|
|
129
|
-
fs_1.default.writeFileSync(`${libPath}/error-handler.js`, errorHandlerJSTemplate({}));
|
|
130
|
-
};
|
|
131
|
-
const generateApiFunctionJSFiles = async (libPath, specifications) => {
|
|
132
|
-
const template = handlebars_1.default.compile(await loadTemplate('api-index.js.hbs'));
|
|
133
|
-
fs_1.default.writeFileSync(`${libPath}/api/index.js`, template({
|
|
134
|
-
specifications,
|
|
135
|
-
}));
|
|
136
|
-
};
|
|
137
|
-
const generateCustomFunctionJSFiles = async (libPath, specifications) => {
|
|
138
|
-
const codeGenerationErrors = [];
|
|
139
|
-
const generateCustomIndexJSTemplate = async () => {
|
|
140
|
-
const customIndexJSTemplate = handlebars_1.default.compile(await loadTemplate('custom-index.js.hbs'));
|
|
141
|
-
fs_1.default.writeFileSync(`${libPath}/client/index.js`, customIndexJSTemplate({
|
|
142
|
-
specifications: specifications.filter(spec => !codeGenerationErrors.find(error => error.specification.id === spec.id)),
|
|
143
|
-
}));
|
|
144
|
-
};
|
|
145
|
-
if (specifications.length === 0) {
|
|
146
|
-
await generateCustomIndexJSTemplate();
|
|
147
|
-
return [];
|
|
148
|
-
}
|
|
149
|
-
const customFunctionJSTemplate = handlebars_1.default.compile(await loadTemplate('custom-function.js.hbs'));
|
|
150
|
-
for (const spec of specifications) {
|
|
151
|
-
try {
|
|
152
|
-
fs_1.default.writeFileSync(`${libPath}/client/${spec.context ? `${spec.context}-` : ''}${spec.name}.js`, await prettyPrint(customFunctionJSTemplate(spec), 'babel'));
|
|
153
|
-
}
|
|
154
|
-
catch (error) {
|
|
155
|
-
codeGenerationErrors.push({
|
|
156
|
-
stack: error.stack,
|
|
157
|
-
specification: spec,
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
await generateCustomIndexJSTemplate();
|
|
162
|
-
return codeGenerationErrors;
|
|
163
|
-
};
|
|
164
|
-
const generateWebhooksJSFiles = async (libPath, specifications) => {
|
|
165
|
-
const template = handlebars_1.default.compile(await loadTemplate('webhooks-index.js.hbs'));
|
|
166
|
-
fs_1.default.writeFileSync(`${libPath}/webhooks/index.js`, template({
|
|
167
|
-
specifications,
|
|
168
|
-
apiKey: getApiKey(),
|
|
169
|
-
}));
|
|
170
|
-
};
|
|
171
|
-
const generateServerFunctionJSFiles = async (libPath, specifications) => {
|
|
172
|
-
const serverIndexJSTemplate = handlebars_1.default.compile(await loadTemplate('server-index.js.hbs'));
|
|
173
|
-
fs_1.default.writeFileSync(`${libPath}/server/index.js`, serverIndexJSTemplate({
|
|
174
|
-
specifications,
|
|
175
|
-
}));
|
|
176
|
-
};
|
|
177
|
-
const generateServerVariableJSFiles = async (libPath, specifications) => {
|
|
178
|
-
const contextData = (0, specs_1.getContextData)(specifications);
|
|
179
|
-
const contextPaths = getContextPaths(contextData);
|
|
180
|
-
const template = handlebars_1.default.compile(await loadTemplate('vari/index.js.hbs'));
|
|
181
|
-
const arrPaths = [];
|
|
182
|
-
for (const specification of specifications) {
|
|
183
|
-
if ((0, utils_1.isPlainObjectPredicate)(specification.variable.value) || Array.isArray(specification.variable.value)) {
|
|
184
|
-
arrPaths.push({
|
|
185
|
-
context: specification.context || '',
|
|
186
|
-
paths: getStringPaths(specification.variable.value),
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
fs_1.default.writeFileSync(`${libPath}/vari/index.js`, template({
|
|
191
|
-
specifications,
|
|
192
|
-
contextPaths,
|
|
193
|
-
apiKey: getApiKey(),
|
|
194
|
-
arrPaths: JSON.stringify(arrPaths),
|
|
195
|
-
}));
|
|
196
|
-
};
|
|
197
|
-
const generateAuthFunctionJSFiles = async (libPath, specifications) => {
|
|
198
|
-
const authIndexJSTemplate = handlebars_1.default.compile(await loadTemplate('auth-index.js.hbs'));
|
|
199
|
-
fs_1.default.writeFileSync(`${libPath}/auth/index.js`, authIndexJSTemplate({
|
|
200
|
-
getTokenFunctions: specifications.filter((spec) => spec.name === 'getToken'),
|
|
201
|
-
subResourceFunctions: specifications.filter((spec) => spec.subResource),
|
|
202
|
-
apiBaseUrl: getApiBaseUrl(),
|
|
203
|
-
apiKey: getApiKey(),
|
|
204
|
-
}));
|
|
205
|
-
if (specifications.length === 0) {
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
const authFunctionJSTemplate = handlebars_1.default.compile(await loadTemplate('auth-function.js.hbs'));
|
|
209
|
-
for (const spec of specifications.filter((spec) => !spec.subResource)) {
|
|
210
|
-
fs_1.default.writeFileSync(`${libPath}/auth/${spec.context ? `${spec.context}-` : ''}${spec.name}.js`, await prettyPrint(authFunctionJSTemplate({
|
|
211
|
-
...spec,
|
|
212
|
-
audienceRequired: spec.function.arguments.some((arg) => arg.name === 'audience'),
|
|
213
|
-
apiBaseUrl: getApiBaseUrl(),
|
|
214
|
-
apiKey: getApiKey(),
|
|
215
|
-
}), 'babel'));
|
|
216
|
-
}
|
|
217
|
-
};
|
|
218
|
-
const generateTSDeclarationFilesForContext = async (libPath, context, contextData, pathPrefix, contextCollector = []) => {
|
|
219
|
-
const contextDataKeys = Object.keys(contextData);
|
|
220
|
-
const contextDataSpecifications = contextDataKeys
|
|
221
|
-
.map((key) => contextData[key])
|
|
222
|
-
.filter((value) => typeof value.type === 'string');
|
|
223
|
-
const contextDataSubContexts = contextDataKeys
|
|
224
|
-
.filter((key) => !contextData[key].type)
|
|
225
|
-
.map((key) => {
|
|
226
|
-
const path = `${context.path ? `${context.path}.` : ''}${key}`;
|
|
227
|
-
return {
|
|
228
|
-
name: key,
|
|
229
|
-
path,
|
|
230
|
-
fileName: `${path}.d.ts`,
|
|
231
|
-
interfaceName: (0, helper_string_1.toPascalCase)(path),
|
|
232
|
-
level: context.level + 1,
|
|
233
|
-
};
|
|
234
|
-
});
|
|
235
|
-
await generateTSContextDeclarationFile(libPath, context, contextDataSpecifications, contextDataSubContexts, pathPrefix);
|
|
236
|
-
contextCollector = [...contextCollector, context];
|
|
237
|
-
for await (const subContext of contextDataSubContexts) {
|
|
238
|
-
contextCollector = await generateTSDeclarationFilesForContext(libPath, subContext, contextData[subContext.name], pathPrefix, contextCollector);
|
|
239
|
-
}
|
|
240
|
-
return contextCollector;
|
|
241
|
-
};
|
|
242
|
-
const assignUnresolvedRefsToPolySchemaRefObj = (schemaDefinition, unresolvedPolySchemaRefs = []) => {
|
|
243
|
-
(0, index_1.iterateRefs)(schemaDefinition, schema => {
|
|
244
|
-
const ref = schema['x-poly-ref'];
|
|
245
|
-
if (ref !== null && typeof ref === 'object' && !Array.isArray(ref)) {
|
|
246
|
-
const foundUnresolved = unresolvedPolySchemaRefs.find(unresolvedPolySchemaRef => unresolvedPolySchemaRef.path === ref.path && unresolvedPolySchemaRef.publicNamespace === ref.publicNamespace);
|
|
247
|
-
if (foundUnresolved) {
|
|
248
|
-
schema['x-poly-ref']['x-unresolved'] = true;
|
|
249
|
-
}
|
|
250
|
-
schema.description = `<path>${ref.publicNamespace ? `${ref.publicNamespace}.${ref.path}` : ref.path}</path>`;
|
|
251
|
-
}
|
|
252
|
-
return schema;
|
|
253
|
-
}, 'x-poly-ref');
|
|
254
|
-
};
|
|
255
|
-
const generateFunctionsTSDeclarationFile = async (libPath, specs) => {
|
|
256
|
-
await generateTSDeclarationFiles(libPath, specs.filter(spec => 'function' in spec).map((spec) => {
|
|
257
|
-
for (const functionArg of spec.function.arguments) {
|
|
258
|
-
if (functionArg.type.kind === 'object' && functionArg.type.schema) {
|
|
259
|
-
assignUnresolvedRefsToPolySchemaRefObj(functionArg.type.schema, functionArg.type.unresolvedPolySchemaRefs);
|
|
260
|
-
}
|
|
261
|
-
else if (functionArg.type.kind === 'object' && functionArg.type.properties) {
|
|
262
|
-
for (const property of functionArg.type.properties) {
|
|
263
|
-
if (property.type.kind === 'object') {
|
|
264
|
-
assignUnresolvedRefsToPolySchemaRefObj(property.type.schema, functionArg.type.unresolvedPolySchemaRefs);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
if (spec.function.returnType.kind === 'object' && spec.function.returnType.schema) {
|
|
270
|
-
assignUnresolvedRefsToPolySchemaRefObj(spec.function.returnType.schema, spec.function.returnType.unresolvedPolySchemaRefs);
|
|
271
|
-
}
|
|
272
|
-
return spec;
|
|
273
|
-
}), 'Poly', '.');
|
|
274
|
-
};
|
|
275
|
-
const generateSchemasTSDeclarationFile = async (libPath, specs) => {
|
|
276
|
-
await generateTSDeclarationFiles(libPath, specs.filter(spec => spec.type === 'schema').map((spec) => {
|
|
277
|
-
assignUnresolvedRefsToPolySchemaRefObj(spec.definition, spec.unresolvedPolySchemaRefs);
|
|
278
|
-
return spec;
|
|
279
|
-
}), 'Schemas', 'schemas');
|
|
280
|
-
};
|
|
281
|
-
const generateVariablesTSDeclarationFile = async (libPath, specs) => await generateTSDeclarationFiles(libPath, specs.filter(spec => 'variable' in spec), 'Vari', 'vari');
|
|
282
|
-
const generateTSDeclarationFiles = async (libPath, specs, interfaceName, pathPrefix) => {
|
|
283
|
-
const contextData = (0, specs_1.getContextData)(specs);
|
|
284
|
-
const contexts = await generateTSDeclarationFilesForContext(libPath, {
|
|
285
|
-
name: '',
|
|
286
|
-
path: '',
|
|
287
|
-
interfaceName,
|
|
288
|
-
fileName: 'default.d.ts',
|
|
289
|
-
level: 0,
|
|
290
|
-
}, contextData, pathPrefix);
|
|
291
|
-
await generateTSIndexDeclarationFile(libPath, contexts, pathPrefix);
|
|
292
|
-
};
|
|
293
|
-
const generateTSIndexDeclarationFile = async (libPath, contexts, pathPrefix) => {
|
|
294
|
-
const template = handlebars_1.default.compile(await loadTemplate(`${pathPrefix}/index.d.ts.hbs`));
|
|
295
|
-
fs_1.default.writeFileSync(`${libPath}/${pathPrefix}/index.d.ts`, await prettyPrint(template({
|
|
296
|
-
contexts: contexts.map((context) => ({
|
|
297
|
-
...context,
|
|
298
|
-
firstLevel: context.level === 1,
|
|
299
|
-
})),
|
|
300
|
-
})));
|
|
301
|
-
};
|
|
302
|
-
const getStringPaths = (data) => {
|
|
303
|
-
const paths = jsonpath_1.default.paths(data, '$..*', 100);
|
|
304
|
-
const stringPaths = [];
|
|
305
|
-
for (let i = 0; i < paths.length; i++) {
|
|
306
|
-
let stringPath = '';
|
|
307
|
-
for (const part of paths[i]) {
|
|
308
|
-
const isString = typeof part === 'string';
|
|
309
|
-
const delimiter = (stringPath.length > 0 && isString) ? '.' : '';
|
|
310
|
-
if (isString) {
|
|
311
|
-
stringPath = `${stringPath}${delimiter}${part}`;
|
|
312
|
-
}
|
|
313
|
-
else {
|
|
314
|
-
stringPath = `${stringPath}${delimiter}[${part}]`;
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
stringPaths.push(stringPath);
|
|
318
|
-
}
|
|
319
|
-
return stringPaths;
|
|
40
|
+
const setGenerationErrors = (value) => {
|
|
41
|
+
generationErrors = value;
|
|
320
42
|
};
|
|
43
|
+
exports.setGenerationErrors = setGenerationErrors;
|
|
44
|
+
const getGenerationErrors = () => generationErrors;
|
|
45
|
+
exports.getGenerationErrors = getGenerationErrors;
|
|
321
46
|
const schemaToDeclarations = async (namespace, typeName, schema, value, options = {
|
|
322
47
|
unknownAny: true,
|
|
323
48
|
}) => {
|
|
324
49
|
const wrapToNamespace = (code) => `namespace ${namespace} {\n ${code}\n}`;
|
|
325
50
|
const appendPathUnionType = (code, value) => {
|
|
326
51
|
if (Array.isArray(value) || (0, utils_1.isPlainObjectPredicate)(value)) {
|
|
327
|
-
const unionPath = getStringPaths(value).map(value => `'${value}'`);
|
|
52
|
+
const unionPath = (0, utils_2.getStringPaths)(value).map(value => `'${value}'`);
|
|
328
53
|
const pathValue = unionPath.join(' | ') || '\'\'';
|
|
329
54
|
return `${code}\nexport type PathValue = ${pathValue}`;
|
|
330
55
|
}
|
|
@@ -419,7 +144,7 @@ const schemaToDeclarations = async (namespace, typeName, schema, value, options
|
|
|
419
144
|
const [, path] = matchPathNameCommentInCode;
|
|
420
145
|
if (['$ReturnType', '$Argument'].includes(argumentOrReturnType)) {
|
|
421
146
|
if (isResolved) {
|
|
422
|
-
const typePath =
|
|
147
|
+
const typePath = `schemas.${path.split('.').map(helper_string_1.toPascalCase).join('.')}`;
|
|
423
148
|
result = result.replace(polySchemaInterfaceDeclaration.code, `export interface ${argumentOrReturnType.replace('$', '')} extends ${typePath} {}`);
|
|
424
149
|
}
|
|
425
150
|
else {
|
|
@@ -452,24 +177,14 @@ const schemaToDeclarations = async (namespace, typeName, schema, value, options
|
|
|
452
177
|
}
|
|
453
178
|
for (const polySchemaTypeReference of polySchemaTypeReferenceList) {
|
|
454
179
|
const polySchemaTypeReferenceParts = getPolySchemaTypeParts(polySchemaTypeReference.name);
|
|
455
|
-
polySchemaTypeReferenceParts
|
|
456
|
-
const [, , , resolvedStatus, ...realContextParts] = polySchemaTypeReferenceParts;
|
|
180
|
+
const [, , , resolvedStatus] = polySchemaTypeReferenceParts;
|
|
457
181
|
const isResolved = resolvedStatus === '$Resolved';
|
|
458
182
|
if (isResolved) {
|
|
459
183
|
const realPathParts = polySchemaTypeReference.path.split('.').map(helper_string_1.toPascalCase);
|
|
460
|
-
realPathParts.
|
|
461
|
-
result = result.replace(polySchemaTypeReference.name, realPathParts.join('.'));
|
|
184
|
+
result = result.replace(polySchemaTypeReference.name, `schemas.${realPathParts.join('.')}`);
|
|
462
185
|
}
|
|
463
186
|
else {
|
|
464
|
-
|
|
465
|
-
const replacementParts = getPolySchemaTypeParts(polySchemaTypeReference.replacement);
|
|
466
|
-
const [, , , , ...realContextParts] = replacementParts;
|
|
467
|
-
realContextParts.push('Schema');
|
|
468
|
-
result = result.replace(polySchemaTypeReference.name, realContextParts.join('$'));
|
|
469
|
-
}
|
|
470
|
-
else {
|
|
471
|
-
result = result.replace(polySchemaTypeReference.name, realContextParts.join('$'));
|
|
472
|
-
}
|
|
187
|
+
result = result.replace(polySchemaTypeReference.name, 'unknown');
|
|
473
188
|
}
|
|
474
189
|
}
|
|
475
190
|
return wrapToNamespace(appendPathUnionType(result, value));
|
|
@@ -506,6 +221,71 @@ const getArgumentsTypeDeclarations = async (namespacePath, parentType, propertie
|
|
|
506
221
|
}
|
|
507
222
|
return typeDeclarations;
|
|
508
223
|
};
|
|
224
|
+
const getIDComment = (specification) => {
|
|
225
|
+
switch (specification.type) {
|
|
226
|
+
case 'apiFunction':
|
|
227
|
+
case 'serverFunction':
|
|
228
|
+
case 'customFunction':
|
|
229
|
+
return `* Function ID: ${specification.id}`;
|
|
230
|
+
case 'authFunction':
|
|
231
|
+
return `* Auth provider ID: ${specification.id}`;
|
|
232
|
+
case 'webhookHandle':
|
|
233
|
+
return `* Webhook ID: ${specification.id}`;
|
|
234
|
+
default:
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
const getAdditionalComments = (specification) => {
|
|
239
|
+
switch (specification.type) {
|
|
240
|
+
case 'customFunction':
|
|
241
|
+
if (!specification.requirements.length) {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
return `This function requires you to have the following libraries installed:\n- ${specification.requirements.join('\n- ')}`;
|
|
245
|
+
default:
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
const getSpecificationWithFunctionComment = (specification) => {
|
|
250
|
+
const descriptionComment = specification.description
|
|
251
|
+
? specification.description
|
|
252
|
+
.split('\n')
|
|
253
|
+
.map((line) => `* ${line}`)
|
|
254
|
+
.join('\n')
|
|
255
|
+
: null;
|
|
256
|
+
const toArgumentComment = (arg, prefix = '') => {
|
|
257
|
+
if (arg.name === 'payload' && arg.type.kind === 'object' && arg.type.properties) {
|
|
258
|
+
return arg.type.properties
|
|
259
|
+
.map(payloadProperty => toArgumentComment(payloadProperty, 'payload.'))
|
|
260
|
+
.filter(Boolean)
|
|
261
|
+
.join('\n');
|
|
262
|
+
}
|
|
263
|
+
if (!arg.description) {
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
return `* @param ${prefix}${arg.name} ${arg.description}`;
|
|
267
|
+
};
|
|
268
|
+
const argumentsComment = specification.function.arguments
|
|
269
|
+
.map(arg => toArgumentComment(arg))
|
|
270
|
+
.filter(Boolean)
|
|
271
|
+
.join('\n');
|
|
272
|
+
const additionalComments = getAdditionalComments(specification);
|
|
273
|
+
const idComment = getIDComment(specification);
|
|
274
|
+
return `${descriptionComment ? `${descriptionComment}\n` : ''}${argumentsComment ? `${argumentsComment}\n` : ''}${additionalComments ? `${additionalComments}\n` : ''}${idComment ? `*\n${idComment}\n` : ''}`.trim();
|
|
275
|
+
};
|
|
276
|
+
const getSpecificationWithVariableComment = (specification) => {
|
|
277
|
+
const descriptionComment = specification.description
|
|
278
|
+
? specification.description
|
|
279
|
+
.split('\n')
|
|
280
|
+
.map((line) => `* ${line}`)
|
|
281
|
+
.join('\n')
|
|
282
|
+
: null;
|
|
283
|
+
const secretComment = specification.variable.secret
|
|
284
|
+
? '* Note: The variable is secret and can be used only within Poly functions.'
|
|
285
|
+
: null;
|
|
286
|
+
const idComment = `* Variable ID: ${specification.id}`;
|
|
287
|
+
return `${descriptionComment ? `${descriptionComment}\n` : ''}${secretComment ? `${secretComment}\n` : ''}${idComment ? `*\n${idComment}` : ''}`.trim();
|
|
288
|
+
};
|
|
509
289
|
const getVariableValueTypeDeclarations = async (namespacePath, namespace, objectProperty, value) => {
|
|
510
290
|
const declarations = await schemaToDeclarations(namespace, 'ValueType', objectProperty.schema, value, {
|
|
511
291
|
unknownAny: false,
|
|
@@ -520,7 +300,7 @@ const getSpecificationsTypeDeclarations = async (namespacePath, specifications)
|
|
|
520
300
|
return await getDeclaration();
|
|
521
301
|
}
|
|
522
302
|
catch (error) {
|
|
523
|
-
|
|
303
|
+
(0, exports.setGenerationErrors)(true);
|
|
524
304
|
errors.push({
|
|
525
305
|
specification,
|
|
526
306
|
stack: error.stack,
|
|
@@ -533,9 +313,21 @@ const getSpecificationsTypeDeclarations = async (namespacePath, specifications)
|
|
|
533
313
|
.map((spec) => spec)
|
|
534
314
|
.map((spec) => getDeclarationOrHandleError(() => getArgumentsTypeDeclarations(namespacePath, (0, helper_string_1.toPascalCase)(spec.name), spec.function.arguments), spec)))).flat();
|
|
535
315
|
const returnTypeDeclarations = await Promise.all(specifications
|
|
536
|
-
.filter((spec) => 'function' in spec &&
|
|
316
|
+
.filter((spec) => 'function' in spec &&
|
|
317
|
+
((spec.function.returnType.kind === 'object' &&
|
|
318
|
+
spec.function.returnType.schema &&
|
|
319
|
+
!(0, specs_1.isBinary)(spec.function.returnType)) ||
|
|
320
|
+
(spec.type === 'serverFunction' && spec.serverSideAsync === true)))
|
|
537
321
|
.map((spec) => spec)
|
|
538
|
-
.map((spec) =>
|
|
322
|
+
.map((spec) => {
|
|
323
|
+
if (spec.type === 'serverFunction' && spec.serverSideAsync === true) {
|
|
324
|
+
const ns = (0, helper_string_1.toPascalCase)(spec.name);
|
|
325
|
+
return Promise.resolve(`namespace ${ns} {\n export type ReturnType = { executionId: string };\n}`);
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
return getDeclarationOrHandleError(() => getObjectTypeDeclarations(namespacePath, (0, helper_string_1.toPascalCase)(spec.name), spec.function.returnType, 'ReturnType'), spec);
|
|
329
|
+
}
|
|
330
|
+
}));
|
|
539
331
|
const variableValueDeclarations = await Promise.all(specifications
|
|
540
332
|
.filter((spec) => 'variable' in spec && spec.variable.valueType.kind === 'object' && spec.variable.valueType.schema)
|
|
541
333
|
.map((spec) => spec)
|
|
@@ -547,13 +339,13 @@ const getSpecificationsTypeDeclarations = async (namespacePath, specifications)
|
|
|
547
339
|
}, 'Schema'), spec)));
|
|
548
340
|
if (errors.length) {
|
|
549
341
|
errors.forEach((err) => {
|
|
550
|
-
echoGenerationError(err.specification);
|
|
342
|
+
(0, utils_2.echoGenerationError)(err.specification);
|
|
551
343
|
});
|
|
552
344
|
}
|
|
553
345
|
return [...argumentsTypeDeclarations, ...returnTypeDeclarations, ...variableValueDeclarations, ...schemaDeclarations].join('\n');
|
|
554
346
|
};
|
|
555
347
|
const generateTSContextDeclarationFile = async (libPath, context, specifications, subContexts, pathPrefix) => {
|
|
556
|
-
const template = handlebars_1.default.compile(
|
|
348
|
+
const template = handlebars_1.default.compile((0, utils_2.loadTemplate)(`${pathPrefix}/{{context}}.d.ts.hbs`));
|
|
557
349
|
const contextPaths = context.path === '' ? [] : context.path.split('.').map(helper_string_1.toPascalCase);
|
|
558
350
|
const typeDeclarations = await getSpecificationsTypeDeclarations(contextPaths.join('.'), specifications);
|
|
559
351
|
const toFunctionDeclaration = (specification) => {
|
|
@@ -573,12 +365,20 @@ const generateTSContextDeclarationFile = async (libPath, context, specifications
|
|
|
573
365
|
}
|
|
574
366
|
return returnType;
|
|
575
367
|
};
|
|
368
|
+
let computedReturnType;
|
|
369
|
+
if (specification.type === 'serverFunction' &&
|
|
370
|
+
specification.serverSideAsync === true) {
|
|
371
|
+
computedReturnType = `${context.interfaceName}.${(0, helper_string_1.toPascalCase)(specification.name)}.ReturnType`;
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
computedReturnType = (0, specs_1.toTypeDeclaration)(specification.function.returnType);
|
|
375
|
+
}
|
|
576
376
|
return {
|
|
577
377
|
name: specification.name.split('.').pop(),
|
|
578
378
|
comment: getSpecificationWithFunctionComment(specification),
|
|
579
379
|
deprecated: specification.state === 'DEPRECATED',
|
|
580
380
|
arguments: specification.function.arguments.map(toArgumentDeclaration),
|
|
581
|
-
returnType: wrapInResponseType(
|
|
381
|
+
returnType: wrapInResponseType(computedReturnType),
|
|
582
382
|
synchronous: specification.type === 'serverFunction' ? false : specification.function.synchronous === true,
|
|
583
383
|
};
|
|
584
384
|
};
|
|
@@ -599,10 +399,11 @@ const generateTSContextDeclarationFile = async (libPath, context, specifications
|
|
|
599
399
|
const contextParts = specification.context.split('.').filter(v => v);
|
|
600
400
|
return {
|
|
601
401
|
name: specification.name.split('.').pop(),
|
|
602
|
-
typeDeclaration: contextParts.length ? `${specification.context.split('.').map(helper_string_1.toPascalCase).join('.')}.${(0, helper_string_1.toPascalCase)(specification.name)}
|
|
402
|
+
typeDeclaration: contextParts.length ? `${specification.context.split('.').map(helper_string_1.toPascalCase).join('.')}.${(0, helper_string_1.toPascalCase)(specification.name)}` : `${(0, helper_string_1.toPascalCase)(specification.name)}`,
|
|
603
403
|
};
|
|
604
404
|
};
|
|
605
|
-
|
|
405
|
+
const outputPath = `${libPath}/${pathPrefix}/${context.fileName}`;
|
|
406
|
+
fs_1.default.writeFileSync(outputPath, await (0, utils_2.prettyPrint)(template({
|
|
606
407
|
interfaceName: context.interfaceName,
|
|
607
408
|
contextPaths,
|
|
608
409
|
typeDeclarations,
|
|
@@ -616,214 +417,84 @@ const generateTSContextDeclarationFile = async (libPath, context, specifications
|
|
|
616
417
|
subContexts,
|
|
617
418
|
})));
|
|
618
419
|
};
|
|
619
|
-
const
|
|
620
|
-
const
|
|
621
|
-
const
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
420
|
+
const generateTSDeclarationFilesForContext = async (libPath, context, contextData, pathPrefix, contextCollector = []) => {
|
|
421
|
+
const contextDataKeys = Object.keys(contextData);
|
|
422
|
+
const contextDataSpecifications = contextDataKeys
|
|
423
|
+
.map((key) => contextData[key])
|
|
424
|
+
.filter((value) => typeof value.type === 'string');
|
|
425
|
+
const contextDataSubContexts = contextDataKeys
|
|
426
|
+
.filter((key) => !contextData[key].type)
|
|
427
|
+
.map((key) => {
|
|
428
|
+
const path = `${context.path ? `${context.path}.` : ''}${key}`;
|
|
429
|
+
return {
|
|
430
|
+
name: key,
|
|
431
|
+
path,
|
|
432
|
+
fileName: `${path}.d.ts`,
|
|
433
|
+
interfaceName: (0, helper_string_1.toPascalCase)(path),
|
|
434
|
+
level: context.level + 1,
|
|
435
|
+
};
|
|
436
|
+
});
|
|
437
|
+
await generateTSContextDeclarationFile(libPath, context, contextDataSpecifications, contextDataSubContexts, pathPrefix);
|
|
438
|
+
contextCollector = [...contextCollector, context];
|
|
439
|
+
for await (const subContext of contextDataSubContexts) {
|
|
440
|
+
contextCollector = await generateTSDeclarationFilesForContext(libPath, subContext, contextData[subContext.name], pathPrefix, contextCollector);
|
|
441
|
+
}
|
|
442
|
+
return contextCollector;
|
|
443
|
+
};
|
|
444
|
+
const assignUnresolvedRefsToPolySchemaRefObj = (schemaDefinition, unresolvedPolySchemaRefs = []) => {
|
|
445
|
+
(0, index_1.iterateRefs)(schemaDefinition, schema => {
|
|
446
|
+
const ref = schema['x-poly-ref'];
|
|
447
|
+
if (ref !== null && typeof ref === 'object' && !Array.isArray(ref)) {
|
|
448
|
+
const foundUnresolved = unresolvedPolySchemaRefs.find(unresolvedPolySchemaRef => unresolvedPolySchemaRef.path === ref.path && unresolvedPolySchemaRef.publicNamespace === ref.publicNamespace);
|
|
449
|
+
if (foundUnresolved) {
|
|
450
|
+
schema['x-poly-ref']['x-unresolved'] = true;
|
|
625
451
|
}
|
|
626
|
-
|
|
627
|
-
paths.push(currentPath);
|
|
628
|
-
traverseAndAddPath(data[key], currentPath);
|
|
452
|
+
schema.description = `<path>${ref.publicNamespace ? `${ref.publicNamespace}.${ref.path}` : ref.path}</path>`;
|
|
629
453
|
}
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
return paths;
|
|
454
|
+
return schema;
|
|
455
|
+
}, 'x-poly-ref');
|
|
633
456
|
};
|
|
634
|
-
const
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
apiFunction: 'API Function',
|
|
645
|
-
customFunction: 'Custom Function',
|
|
646
|
-
authFunction: 'Auth Function',
|
|
647
|
-
webhookHandle: 'Webhook Handle',
|
|
648
|
-
serverFunction: 'Server Function',
|
|
649
|
-
serverVariable: 'Variable',
|
|
650
|
-
schema: 'Schema',
|
|
651
|
-
snippet: 'Snippet',
|
|
652
|
-
};
|
|
653
|
-
const type = typeMap[specification.type];
|
|
654
|
-
shelljs_1.default.echo(chalk_1.default.red(`\nError encountered while processing ${type} '${name}' (id: '${specification.id}'). ${type} is unavailable.`));
|
|
457
|
+
const generateTSDeclarationFiles = async (libPath, specs, interfaceName, pathPrefix) => {
|
|
458
|
+
const contextData = (0, specs_1.getContextData)(specs);
|
|
459
|
+
const contexts = await generateTSDeclarationFilesForContext(libPath, {
|
|
460
|
+
name: '',
|
|
461
|
+
path: '',
|
|
462
|
+
interfaceName,
|
|
463
|
+
fileName: 'default.d.ts',
|
|
464
|
+
level: 0,
|
|
465
|
+
}, contextData, pathPrefix);
|
|
466
|
+
await generateTSIndexDeclarationFile(libPath, contexts, pathPrefix);
|
|
655
467
|
};
|
|
656
|
-
const
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
468
|
+
const generateTSIndexDeclarationFile = async (libPath, contexts, pathPrefix) => {
|
|
469
|
+
const template = handlebars_1.default.compile((0, utils_2.loadTemplate)(`${pathPrefix}/index.d.ts.hbs`));
|
|
470
|
+
fs_1.default.writeFileSync(`${libPath}/${pathPrefix}/index.d.ts`, await (0, utils_2.prettyPrint)(template({
|
|
471
|
+
contexts: contexts.map((context) => ({
|
|
472
|
+
...context,
|
|
473
|
+
firstLevel: context.level === 1,
|
|
474
|
+
})),
|
|
475
|
+
})));
|
|
662
476
|
};
|
|
663
|
-
const
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
contextData = (0, utils_2.getContextDataFileContent)(libPath);
|
|
669
|
-
}
|
|
670
|
-
catch (error) {
|
|
671
|
-
shelljs_1.default.echo(chalk_1.default.red('ERROR'));
|
|
672
|
-
shelljs_1.default.echo('Error while fetching local context data.');
|
|
673
|
-
shelljs_1.default.echo(chalk_1.default.red(error.message));
|
|
674
|
-
shelljs_1.default.echo(chalk_1.default.red(error.stack));
|
|
675
|
-
return;
|
|
676
|
-
}
|
|
677
|
-
const prevSpecs = (0, utils_2.getSpecsFromContextData)(contextData);
|
|
678
|
-
let specs = [];
|
|
679
|
-
try {
|
|
680
|
-
specs = await (0, api_1.getSpecs)([], [], [functionId]);
|
|
681
|
-
}
|
|
682
|
-
catch (error) {
|
|
683
|
-
(0, utils_2.showErrGettingSpecs)(error);
|
|
684
|
-
return;
|
|
685
|
-
}
|
|
686
|
-
const [customFunction] = specs;
|
|
687
|
-
if (prevSpecs.some((prevSpec) => prevSpec.id === customFunction.id)) {
|
|
688
|
-
specs = prevSpecs.map((prevSpec) => {
|
|
689
|
-
if (prevSpec.id === customFunction.id) {
|
|
690
|
-
return customFunction;
|
|
477
|
+
const generateFunctionsTSDeclarationFile = async (libPath, specs) => {
|
|
478
|
+
await generateTSDeclarationFiles(libPath, specs.filter(spec => 'function' in spec).map((spec) => {
|
|
479
|
+
for (const functionArg of spec.function.arguments) {
|
|
480
|
+
if (functionArg.type.kind === 'object' && functionArg.type.schema) {
|
|
481
|
+
assignUnresolvedRefsToPolySchemaRefObj(functionArg.type.schema, functionArg.type.unresolvedPolySchemaRefs);
|
|
691
482
|
}
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
}
|
|
699
|
-
await prepareDir(polyPath);
|
|
700
|
-
generationErrors = false;
|
|
701
|
-
await (0, exports.generateSpecs)(libPath, specs, false);
|
|
702
|
-
if (generationErrors) {
|
|
703
|
-
shelljs_1.default.echo(chalk_1.default.yellow('Generate DONE with errors. Please investigate the errors and contact support@polyapi.io for assistance.'));
|
|
704
|
-
}
|
|
705
|
-
else {
|
|
706
|
-
shelljs_1.default.echo(chalk_1.default.green('DONE'));
|
|
707
|
-
}
|
|
708
|
-
};
|
|
709
|
-
exports.generateSingleCustomFunction = generateSingleCustomFunction;
|
|
710
|
-
const getIDComment = (specification) => {
|
|
711
|
-
switch (specification.type) {
|
|
712
|
-
case 'apiFunction':
|
|
713
|
-
case 'serverFunction':
|
|
714
|
-
case 'customFunction':
|
|
715
|
-
return `* Function ID: ${specification.id}`;
|
|
716
|
-
case 'authFunction':
|
|
717
|
-
return `* Auth provider ID: ${specification.id}`;
|
|
718
|
-
case 'webhookHandle':
|
|
719
|
-
return `* Webhook ID: ${specification.id}`;
|
|
720
|
-
default:
|
|
721
|
-
return null;
|
|
722
|
-
}
|
|
723
|
-
};
|
|
724
|
-
const getSpecificationWithFunctionComment = (specification) => {
|
|
725
|
-
const descriptionComment = specification.description
|
|
726
|
-
? specification.description
|
|
727
|
-
.split('\n')
|
|
728
|
-
.map((line) => `* ${line}`)
|
|
729
|
-
.join('\n')
|
|
730
|
-
: null;
|
|
731
|
-
const toArgumentComment = (arg, prefix = '') => {
|
|
732
|
-
if (arg.name === 'payload' && arg.type.kind === 'object' && arg.type.properties) {
|
|
733
|
-
return arg.type.properties
|
|
734
|
-
.map(payloadProperty => toArgumentComment(payloadProperty, 'payload.'))
|
|
735
|
-
.filter(Boolean)
|
|
736
|
-
.join('\n');
|
|
737
|
-
}
|
|
738
|
-
if (!arg.description) {
|
|
739
|
-
return null;
|
|
740
|
-
}
|
|
741
|
-
return `* @param ${prefix}${arg.name} ${arg.description}`;
|
|
742
|
-
};
|
|
743
|
-
const argumentsComment = specification.function.arguments
|
|
744
|
-
.map(arg => toArgumentComment(arg))
|
|
745
|
-
.filter(Boolean)
|
|
746
|
-
.join('\n');
|
|
747
|
-
const additionalComments = getAdditionalComments(specification);
|
|
748
|
-
const idComment = getIDComment(specification);
|
|
749
|
-
return `${descriptionComment ? `${descriptionComment}\n` : ''}${argumentsComment ? `${argumentsComment}\n` : ''}${additionalComments ? `${additionalComments}\n` : ''}${idComment ? `*\n${idComment}\n` : ''}`.trim();
|
|
750
|
-
};
|
|
751
|
-
const getAdditionalComments = (specification) => {
|
|
752
|
-
switch (specification.type) {
|
|
753
|
-
case 'customFunction':
|
|
754
|
-
if (!specification.requirements.length) {
|
|
755
|
-
return null;
|
|
483
|
+
else if (functionArg.type.kind === 'object' && functionArg.type.properties) {
|
|
484
|
+
for (const property of functionArg.type.properties) {
|
|
485
|
+
if (property.type.kind === 'object') {
|
|
486
|
+
assignUnresolvedRefsToPolySchemaRefObj(property.type.schema, functionArg.type.unresolvedPolySchemaRefs);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
756
489
|
}
|
|
757
|
-
return `This function requires you to have the following libraries installed:\n- ${specification.requirements.join('\n- ')}`;
|
|
758
|
-
default:
|
|
759
|
-
return null;
|
|
760
|
-
}
|
|
761
|
-
};
|
|
762
|
-
const getSpecificationWithVariableComment = (specification) => {
|
|
763
|
-
const descriptionComment = specification.description
|
|
764
|
-
? specification.description
|
|
765
|
-
.split('\n')
|
|
766
|
-
.map((line) => `* ${line}`)
|
|
767
|
-
.join('\n')
|
|
768
|
-
: null;
|
|
769
|
-
const secretComment = specification.variable.secret
|
|
770
|
-
? '* Note: The variable is secret and can be used only within Poly functions.'
|
|
771
|
-
: null;
|
|
772
|
-
const idComment = `* Variable ID: ${specification.id}`;
|
|
773
|
-
return `${descriptionComment ? `${descriptionComment}\n` : ''}${secretComment ? `${secretComment}\n` : ''}${idComment ? `*\n${idComment}` : ''}`.trim();
|
|
774
|
-
};
|
|
775
|
-
const generate = async ({ polyPath, contexts, names, functionIds, noTypes, }) => {
|
|
776
|
-
let specs = [];
|
|
777
|
-
const generateMsg = contexts ? `Generating Poly TypeScript SDK for contexts "${contexts}"...` : 'Generating Poly TypeScript SDK...';
|
|
778
|
-
shelljs_1.default.echo('-n', generateMsg);
|
|
779
|
-
await prepareDir(polyPath);
|
|
780
|
-
(0, config_1.loadConfig)(polyPath);
|
|
781
|
-
try {
|
|
782
|
-
specs = await (0, api_1.getSpecs)(contexts, names, functionIds);
|
|
783
|
-
}
|
|
784
|
-
catch (error) {
|
|
785
|
-
(0, utils_2.showErrGettingSpecs)(error);
|
|
786
|
-
return;
|
|
787
|
-
}
|
|
788
|
-
generationErrors = false;
|
|
789
|
-
await (0, exports.generateSpecs)((0, utils_2.getPolyLibPath)(polyPath), specs, noTypes);
|
|
790
|
-
if (generationErrors) {
|
|
791
|
-
shelljs_1.default.echo(chalk_1.default.yellow('Generate DONE with errors. Please investigate the errors and contact support@polyapi.io for assistance.'));
|
|
792
|
-
}
|
|
793
|
-
else {
|
|
794
|
-
shelljs_1.default.echo(chalk_1.default.green('DONE'));
|
|
795
|
-
}
|
|
796
|
-
};
|
|
797
|
-
exports.generate = generate;
|
|
798
|
-
const generateSpecs = async (libPath, specs, noTypes) => {
|
|
799
|
-
try {
|
|
800
|
-
let missingNames = [];
|
|
801
|
-
[missingNames, specs] = specs.reduce((acc, s) => {
|
|
802
|
-
acc[s.name.trim() ? 1 : 0].push(s);
|
|
803
|
-
return acc;
|
|
804
|
-
}, [[], []]);
|
|
805
|
-
const jsFilesCodeGenerationErrors = await generateJSFiles(libPath, specs);
|
|
806
|
-
const filteredSpecs = specs.filter(spec => !jsFilesCodeGenerationErrors.find(codeGenerationError => codeGenerationError.specification.id === spec.id));
|
|
807
|
-
if (!noTypes) {
|
|
808
|
-
await generateFunctionsTSDeclarationFile(libPath, filteredSpecs);
|
|
809
|
-
await generateVariablesTSDeclarationFile(libPath, filteredSpecs);
|
|
810
|
-
await generateSchemasTSDeclarationFile(libPath, filteredSpecs);
|
|
811
|
-
}
|
|
812
|
-
(0, utils_2.generateContextDataFile)(libPath, filteredSpecs);
|
|
813
|
-
if (missingNames.length) {
|
|
814
|
-
generationErrors = true;
|
|
815
|
-
missingNames.map((s) => echoGenerationError(s));
|
|
816
490
|
}
|
|
817
|
-
if (
|
|
818
|
-
|
|
819
|
-
jsFilesCodeGenerationErrors.forEach((error) => {
|
|
820
|
-
echoGenerationError(error.specification);
|
|
821
|
-
});
|
|
491
|
+
if (spec.function.returnType.kind === 'object' && spec.function.returnType.schema) {
|
|
492
|
+
assignUnresolvedRefsToPolySchemaRefObj(spec.function.returnType.schema, spec.function.returnType.unresolvedPolySchemaRefs);
|
|
822
493
|
}
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
showErrGeneratingFiles(error);
|
|
826
|
-
}
|
|
494
|
+
return spec;
|
|
495
|
+
}), 'Poly', '.');
|
|
827
496
|
};
|
|
828
|
-
exports.
|
|
829
|
-
|
|
497
|
+
exports.generateFunctionsTSDeclarationFile = generateFunctionsTSDeclarationFile;
|
|
498
|
+
const generateVariablesTSDeclarationFile = async (libPath, specs) => await generateTSDeclarationFiles(libPath, specs.filter(spec => 'variable' in spec), 'Vari', 'vari');
|
|
499
|
+
exports.generateVariablesTSDeclarationFile = generateVariablesTSDeclarationFile;
|
|
500
|
+
//# sourceMappingURL=types.js.map
|