@chargebee/code-sample-generator 0.3.6 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.js +11 -11
- package/dist/core/ir-builder.d.ts.map +1 -1
- package/dist/core/validation.d.ts.map +1 -1
- package/dist/{generate-from-sample-DX6FoZAo.js → generate-from-sample-DhbfCFUp.js} +319 -19
- package/dist/{generate-from-sample-DX6FoZAo.js.map → generate-from-sample-DhbfCFUp.js.map} +1 -1
- package/dist/index.js +51 -254
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +8 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/generate-code-sample.d.ts +9 -0
- package/dist/utils/generate-code-sample.d.ts.map +1 -1
- package/dist/utils/generate-from-sample.d.ts.map +1 -1
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { g as generateFromSample, a as getGenerator, L as LanguageNotSupportedError, b as getSupportedLanguages, c as getOperation, O as OperationNotFoundError, e as expandDescriptor, d as buildIR, C as CodeGenerationError, p as pluralize } from "./generate-from-sample-
|
|
2
|
-
import {
|
|
1
|
+
import { g as generateFromSample, a as getGenerator, L as LanguageNotSupportedError, b as getSupportedLanguages, c as getOperation, O as OperationNotFoundError, e as expandDescriptor, d as assertValid, f as buildIR, C as CodeGenerationError, p as pluralize } from "./generate-from-sample-DhbfCFUp.js";
|
|
2
|
+
import { n, A, i, j, k, h, D, o, G, q, l, T, V, w, x, r, t, s, v, y, u, m, z } from "./generate-from-sample-DhbfCFUp.js";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import "node:url";
|
|
5
5
|
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
@@ -69,240 +69,6 @@ const LANGUAGES = [
|
|
|
69
69
|
"go",
|
|
70
70
|
"dotnet"
|
|
71
71
|
];
|
|
72
|
-
class ValidationError extends Error {
|
|
73
|
-
constructor(operation, issues) {
|
|
74
|
-
const summary = issues.map((i2) => `${i2.path}: ${i2.message}`).join("; ");
|
|
75
|
-
super(`Validation failed for ${operation}: ${summary}`);
|
|
76
|
-
this.operation = operation;
|
|
77
|
-
this.issues = issues;
|
|
78
|
-
if (Error.captureStackTrace) {
|
|
79
|
-
Error.captureStackTrace(this, ValidationError);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
name = "ValidationError";
|
|
83
|
-
}
|
|
84
|
-
function validateParams(schema, params, options = {}) {
|
|
85
|
-
const opts = {
|
|
86
|
-
allowUnknown: options.allowUnknown ?? true,
|
|
87
|
-
// LENIENT by default
|
|
88
|
-
validateEnums: options.validateEnums ?? true,
|
|
89
|
-
validateTypes: options.validateTypes ?? true,
|
|
90
|
-
validateRequired: options.validateRequired ?? true
|
|
91
|
-
};
|
|
92
|
-
const issues = [];
|
|
93
|
-
validateObject(schema, params, "", issues, opts);
|
|
94
|
-
return issues;
|
|
95
|
-
}
|
|
96
|
-
function validateObject(schema, obj, basePath, issues, opts) {
|
|
97
|
-
if (opts.validateRequired) {
|
|
98
|
-
for (const requiredField of schema.required ?? []) {
|
|
99
|
-
const value = obj[requiredField];
|
|
100
|
-
if (value === void 0 || value === null) {
|
|
101
|
-
const path = joinPath(basePath, requiredField);
|
|
102
|
-
issues.push({
|
|
103
|
-
path,
|
|
104
|
-
message: `Required field "${requiredField}" is missing`,
|
|
105
|
-
code: "required"
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
111
|
-
if (value === void 0) continue;
|
|
112
|
-
const fieldPath = joinPath(basePath, key);
|
|
113
|
-
const propSchema = schema.properties?.[key];
|
|
114
|
-
if (!propSchema) {
|
|
115
|
-
if (!opts.allowUnknown) {
|
|
116
|
-
issues.push({
|
|
117
|
-
path: fieldPath,
|
|
118
|
-
message: `Unknown field "${key}"`,
|
|
119
|
-
code: "unknown_field"
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
if (value === null) continue;
|
|
125
|
-
if (opts.validateTypes) {
|
|
126
|
-
const typeIssue = validateType(fieldPath, value, propSchema);
|
|
127
|
-
if (typeIssue) {
|
|
128
|
-
issues.push(typeIssue);
|
|
129
|
-
continue;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
if (opts.validateEnums && propSchema.enum) {
|
|
133
|
-
const stringValue = String(value);
|
|
134
|
-
if (!propSchema.enum.includes(stringValue)) {
|
|
135
|
-
issues.push({
|
|
136
|
-
path: fieldPath,
|
|
137
|
-
message: `Invalid value "${stringValue}". Valid values: ${propSchema.enum.join(", ")}`,
|
|
138
|
-
code: "enum"
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
if (propSchema.type === "object" && propSchema.properties && typeof value === "object" && !Array.isArray(value)) {
|
|
143
|
-
validateObject(
|
|
144
|
-
propSchema,
|
|
145
|
-
value,
|
|
146
|
-
fieldPath,
|
|
147
|
-
issues,
|
|
148
|
-
opts
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
if (propSchema.type === "array" && Array.isArray(value)) {
|
|
152
|
-
validateArray(propSchema, value, fieldPath, issues, opts);
|
|
153
|
-
}
|
|
154
|
-
if (propSchema.isFilter && typeof value === "object" && !Array.isArray(value)) {
|
|
155
|
-
validateFilterParam(
|
|
156
|
-
propSchema,
|
|
157
|
-
value,
|
|
158
|
-
fieldPath,
|
|
159
|
-
issues,
|
|
160
|
-
opts
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
function validateArray(schema, arr, basePath, issues, opts) {
|
|
166
|
-
const itemSchema = schema.items;
|
|
167
|
-
if (!itemSchema) return;
|
|
168
|
-
arr.forEach((item, index) => {
|
|
169
|
-
const itemPath = `${basePath}[${index}]`;
|
|
170
|
-
if (item === null || item === void 0) return;
|
|
171
|
-
if (opts.validateTypes) {
|
|
172
|
-
const typeIssue = validateType(itemPath, item, itemSchema);
|
|
173
|
-
if (typeIssue) {
|
|
174
|
-
issues.push(typeIssue);
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
if (itemSchema.type === "object" && itemSchema.properties && typeof item === "object" && !Array.isArray(item)) {
|
|
179
|
-
validateObject(
|
|
180
|
-
itemSchema,
|
|
181
|
-
item,
|
|
182
|
-
itemPath,
|
|
183
|
-
issues,
|
|
184
|
-
opts
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
function validateFilterParam(schema, filterValue, basePath, issues, opts) {
|
|
190
|
-
const validOperators = /* @__PURE__ */ new Set([
|
|
191
|
-
"is",
|
|
192
|
-
"is_not",
|
|
193
|
-
"in",
|
|
194
|
-
"not_in",
|
|
195
|
-
"gt",
|
|
196
|
-
"gte",
|
|
197
|
-
"lt",
|
|
198
|
-
"lte",
|
|
199
|
-
"between",
|
|
200
|
-
"on",
|
|
201
|
-
"before",
|
|
202
|
-
"after",
|
|
203
|
-
"starts_with",
|
|
204
|
-
"is_present",
|
|
205
|
-
"is_not_present"
|
|
206
|
-
]);
|
|
207
|
-
for (const [operator, value] of Object.entries(filterValue)) {
|
|
208
|
-
if (!validOperators.has(operator) && !opts.allowUnknown) {
|
|
209
|
-
issues.push({
|
|
210
|
-
path: `${basePath}[${operator}]`,
|
|
211
|
-
message: `Unknown filter operator "${operator}"`,
|
|
212
|
-
code: "unknown_field"
|
|
213
|
-
});
|
|
214
|
-
continue;
|
|
215
|
-
}
|
|
216
|
-
if (opts.validateEnums && schema.enum && schema.filterType === "enum") {
|
|
217
|
-
const values = Array.isArray(value) ? value : [value];
|
|
218
|
-
for (const v2 of values) {
|
|
219
|
-
if (!schema.enum.includes(String(v2))) {
|
|
220
|
-
issues.push({
|
|
221
|
-
path: `${basePath}[${operator}]`,
|
|
222
|
-
message: `Invalid enum value "${v2}". Valid values: ${schema.enum.join(", ")}`,
|
|
223
|
-
code: "enum"
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
function validateType(path, value, schema) {
|
|
231
|
-
const actualType = getValueType(value);
|
|
232
|
-
switch (schema.type) {
|
|
233
|
-
case "string":
|
|
234
|
-
if (actualType !== "string") {
|
|
235
|
-
return {
|
|
236
|
-
path,
|
|
237
|
-
message: `Expected string, got ${actualType}`,
|
|
238
|
-
code: "type"
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
break;
|
|
242
|
-
case "integer":
|
|
243
|
-
case "number":
|
|
244
|
-
if (actualType !== "number") {
|
|
245
|
-
return {
|
|
246
|
-
path,
|
|
247
|
-
message: `Expected number, got ${actualType}`,
|
|
248
|
-
code: "type"
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
if (schema.type === "integer" && !Number.isInteger(value)) {
|
|
252
|
-
return {
|
|
253
|
-
path,
|
|
254
|
-
message: `Expected integer, got decimal number`,
|
|
255
|
-
code: "type"
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
break;
|
|
259
|
-
case "boolean":
|
|
260
|
-
if (actualType !== "boolean") {
|
|
261
|
-
return {
|
|
262
|
-
path,
|
|
263
|
-
message: `Expected boolean, got ${actualType}`,
|
|
264
|
-
code: "type"
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
break;
|
|
268
|
-
case "array":
|
|
269
|
-
if (actualType !== "array") {
|
|
270
|
-
return {
|
|
271
|
-
path,
|
|
272
|
-
message: `Expected array, got ${actualType}`,
|
|
273
|
-
code: "type"
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
break;
|
|
277
|
-
case "object":
|
|
278
|
-
if (actualType !== "object") {
|
|
279
|
-
return {
|
|
280
|
-
path,
|
|
281
|
-
message: `Expected object, got ${actualType}`,
|
|
282
|
-
code: "type"
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
|
-
break;
|
|
286
|
-
}
|
|
287
|
-
return null;
|
|
288
|
-
}
|
|
289
|
-
function getValueType(value) {
|
|
290
|
-
if (value === null) return "null";
|
|
291
|
-
if (Array.isArray(value)) return "array";
|
|
292
|
-
return typeof value;
|
|
293
|
-
}
|
|
294
|
-
function joinPath(base, field) {
|
|
295
|
-
return base ? `${base}.${field}` : field;
|
|
296
|
-
}
|
|
297
|
-
function assertValid(operationId, schema, params, options) {
|
|
298
|
-
const issues = validateParams(schema, params, options);
|
|
299
|
-
if (issues.length > 0) {
|
|
300
|
-
throw new ValidationError(operationId, issues);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
function isValid(schema, params, options) {
|
|
304
|
-
return validateParams(schema, params, options).length === 0;
|
|
305
|
-
}
|
|
306
72
|
function toPascalCase(str) {
|
|
307
73
|
return str.split(/[-_]/).map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join("");
|
|
308
74
|
}
|
|
@@ -520,6 +286,16 @@ async function generateCodeSample(options) {
|
|
|
520
286
|
params = reqParams.map ? reqParams.map : reqParams;
|
|
521
287
|
note = sampleRequest.note;
|
|
522
288
|
}
|
|
289
|
+
const shouldValidate = options.validate ?? true;
|
|
290
|
+
if (shouldValidate) {
|
|
291
|
+
const validationOpts = typeof shouldValidate === "object" ? shouldValidate : { allowUnknown: false };
|
|
292
|
+
const bodyParams = stripPathParams(
|
|
293
|
+
params,
|
|
294
|
+
descriptor.http.path,
|
|
295
|
+
descriptor.params
|
|
296
|
+
);
|
|
297
|
+
assertValid(operation, descriptor.params, bodyParams, validationOpts);
|
|
298
|
+
}
|
|
523
299
|
const ir = buildIR(descriptor, params, {
|
|
524
300
|
site: site || "{site}",
|
|
525
301
|
apiKey: apiKey || "{site_api_key}",
|
|
@@ -577,6 +353,27 @@ function getCombinedVersion(apiVersion, pcVersion) {
|
|
|
577
353
|
}
|
|
578
354
|
return "v2-pcv2";
|
|
579
355
|
}
|
|
356
|
+
function stripPathParams(params, pathTemplate, bodySchema) {
|
|
357
|
+
const pathParamKeys = /* @__PURE__ */ new Set();
|
|
358
|
+
const placeholderRegex = /\{([^}]+)\}/g;
|
|
359
|
+
let match = placeholderRegex.exec(pathTemplate);
|
|
360
|
+
while (match !== null) {
|
|
361
|
+
const name = match[1];
|
|
362
|
+
pathParamKeys.add(name);
|
|
363
|
+
pathParamKeys.add(name.replace(/-/g, "_"));
|
|
364
|
+
if (name.endsWith("-id") || name.endsWith("_id")) {
|
|
365
|
+
pathParamKeys.add("id");
|
|
366
|
+
}
|
|
367
|
+
match = placeholderRegex.exec(pathTemplate);
|
|
368
|
+
}
|
|
369
|
+
const result = {};
|
|
370
|
+
for (const [key, value] of Object.entries(params)) {
|
|
371
|
+
if (bodySchema.properties?.[key] !== void 0 || !pathParamKeys.has(key)) {
|
|
372
|
+
result[key] = value;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return result;
|
|
376
|
+
}
|
|
580
377
|
const SAMPLES_BASE_DIR = "samples/cb-app";
|
|
581
378
|
const REQUEST_SUBDIR = "request";
|
|
582
379
|
const RESPONSE_SUBDIR = "response";
|
|
@@ -948,28 +745,28 @@ function fetchAllRequestSamples(resource, operation, apiVersion, pcVersion) {
|
|
|
948
745
|
}
|
|
949
746
|
}
|
|
950
747
|
export {
|
|
951
|
-
|
|
748
|
+
n as AVAILABLE_LANGUAGES,
|
|
952
749
|
A as AVAILABLE_LANGUAGE_IDS,
|
|
953
|
-
|
|
954
|
-
|
|
750
|
+
i as Carousel,
|
|
751
|
+
j as CodeBlock,
|
|
955
752
|
CodeGenerationError,
|
|
956
|
-
|
|
957
|
-
|
|
753
|
+
k as CodeSampleHeader,
|
|
754
|
+
h as CodeSamples,
|
|
958
755
|
D as DEFAULT_LANGUAGE,
|
|
959
|
-
|
|
756
|
+
o as DEFAULT_THEME,
|
|
960
757
|
G as GoToApiExplorer,
|
|
961
758
|
LANGUAGES,
|
|
962
|
-
|
|
759
|
+
q as LANGUAGE_DISPLAY_NAMES,
|
|
963
760
|
LanguageNotSupportedError,
|
|
964
|
-
|
|
761
|
+
l as LanguageSwitcher,
|
|
965
762
|
OpenAPIExtensions,
|
|
966
763
|
OperationNotFoundError,
|
|
967
764
|
T as TryInApiExplorer,
|
|
968
|
-
ValidationError,
|
|
765
|
+
V as ValidationError,
|
|
969
766
|
addCopyButton,
|
|
970
767
|
assertValid,
|
|
971
768
|
buildIR,
|
|
972
|
-
|
|
769
|
+
w as compressDescriptor,
|
|
973
770
|
enrichDescriptorWithOpenAPI,
|
|
974
771
|
enrichDescriptorsWithOpenAPI,
|
|
975
772
|
expandDescriptor,
|
|
@@ -977,7 +774,7 @@ export {
|
|
|
977
774
|
extractReturnsFromOpenAPI,
|
|
978
775
|
fetchAllRequestSamples,
|
|
979
776
|
findOperationById,
|
|
980
|
-
|
|
777
|
+
x as formatValue,
|
|
981
778
|
generateCodeSample,
|
|
982
779
|
generateCodeSamples,
|
|
983
780
|
generateCodeSamplesBatch,
|
|
@@ -986,14 +783,14 @@ export {
|
|
|
986
783
|
getFirstAvailableOperationResponse,
|
|
987
784
|
getGenerator,
|
|
988
785
|
getOperation,
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
786
|
+
r as getOperationIds,
|
|
787
|
+
t as getOperationIdsForVersion,
|
|
788
|
+
s as getOperations,
|
|
992
789
|
getSupportedLanguages,
|
|
993
|
-
|
|
994
|
-
isValid,
|
|
790
|
+
v as hasGenerator,
|
|
791
|
+
y as isValid,
|
|
995
792
|
u as useLanguageSelection,
|
|
996
|
-
|
|
997
|
-
validateParams
|
|
793
|
+
m as useSampleLoader,
|
|
794
|
+
z as validateParams
|
|
998
795
|
};
|
|
999
796
|
//# sourceMappingURL=index.js.map
|