api-farmer 0.0.23 → 0.0.24

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 CHANGED
@@ -148,6 +148,10 @@ export interface Config {
148
148
  * The transformer api options, used to override the default transformation rules.
149
149
  */
150
150
  transformer?: Partial<Transformer>
151
+ /**
152
+ * Certain uncountable nouns that do not change from singular to plural
153
+ */
154
+ uncountableNouns?: string[]
151
155
  }
152
156
  ```
153
157
 
@@ -53,7 +53,7 @@ function transformVerb({ method }) {
53
53
  return pascalCase(method);
54
54
  }
55
55
  }
56
- function transformEntity({ path, method, base }) {
56
+ function transformEntity({ path, method, base, uncountableNouns }) {
57
57
  path = base ? path.replace(base, "") : path;
58
58
  const words = path.split("/").filter(Boolean);
59
59
  return words.reduce((entity, word, index) => {
@@ -61,9 +61,9 @@ function transformEntity({ path, method, base }) {
61
61
  return entity;
62
62
  }
63
63
  word = word.replace(/\.([a-z])/g, (_, p) => p.toUpperCase());
64
- word = pluralize.singular(pascalCase(word));
64
+ word = uncountableNouns.includes(word) ? word : pluralize.singular(pascalCase(word));
65
65
  if (method === "get" && index === words.length - 1) {
66
- word = pluralize.plural(word);
66
+ word = uncountableNouns.includes(word) ? `${word}List` : pluralize.plural(word);
67
67
  }
68
68
  return `${entity}${word}`;
69
69
  }, "");
@@ -128,10 +128,10 @@ function createTransformer() {
128
128
 
129
129
  // src/generate.ts
130
130
  function transformPayloads(pathItems, options) {
131
- const { transformer, path, base, validateStatus } = options;
131
+ const { transformer, path, base, uncountableNouns, validateStatus } = options;
132
132
  return Object.entries(pathItems).filter(([key]) => SUPPORTED_HTTP_METHODS.includes(key)).reduce((payloads, [method, operation]) => {
133
133
  const url = transformer.url({ path, base });
134
- const args = { path, base, url, method, operation };
134
+ const args = { path, base, url, method, uncountableNouns, operation };
135
135
  const entity = transformer.entity(args);
136
136
  const verb = transformer.verb(args);
137
137
  const requestContentType = operation.requestBody ? getRequestBodyContentType(operation.requestBody) : void 0;
@@ -172,7 +172,7 @@ function transformPayloads(pathItems, options) {
172
172
  }, []);
173
173
  }
174
174
  function partitionApiModules(schema, options) {
175
- const { base, transformer, validateStatus } = options;
175
+ const { base, transformer, uncountableNouns, validateStatus } = options;
176
176
  const schemaPaths = schema.paths ?? {};
177
177
  const schemaPathKeys = base ? Object.keys(schemaPaths).map((key) => key.replace(base, "")) : Object.keys(schemaPaths);
178
178
  const keyToPaths = groupBy(schemaPathKeys, (key) => key.split("/")[1]);
@@ -180,7 +180,13 @@ function partitionApiModules(schema, options) {
180
180
  const payloads = paths.reduce((payloads2, path) => {
181
181
  const pathItems = schemaPaths[path];
182
182
  payloads2.push(
183
- ...transformPayloads(pathItems, { ...options, path: base ? base + path : path, transformer, validateStatus })
183
+ ...transformPayloads(pathItems, {
184
+ ...options,
185
+ path: base ? base + path : path,
186
+ transformer,
187
+ uncountableNouns,
188
+ validateStatus
189
+ })
184
190
  );
185
191
  return payloads2;
186
192
  }, []);
@@ -243,7 +249,8 @@ async function generate(userOptions = {}) {
243
249
  output = "./src/apis/generated",
244
250
  typesFilename = "_types.ts",
245
251
  validateStatus = (status) => status >= 200 && status < 300,
246
- transformer = {}
252
+ transformer = {},
253
+ uncountableNouns = []
247
254
  } = options;
248
255
  const mergedTransformer = { ...createTransformer(), ...transformer };
249
256
  const schema = await readSchema(input);
@@ -253,6 +260,7 @@ async function generate(userOptions = {}) {
253
260
  }
254
261
  const apiModules = partitionApiModules(schema, {
255
262
  base,
263
+ uncountableNouns,
256
264
  transformer: mergedTransformer,
257
265
  validateStatus
258
266
  });
package/dist/cli.cjs CHANGED
@@ -166,7 +166,7 @@ function transformVerb({ method }) {
166
166
  return (0, import_rattail2.pascalCase)(method);
167
167
  }
168
168
  }
169
- function transformEntity({ path, method, base }) {
169
+ function transformEntity({ path, method, base, uncountableNouns }) {
170
170
  path = base ? path.replace(base, "") : path;
171
171
  const words = path.split("/").filter(Boolean);
172
172
  return words.reduce((entity, word, index) => {
@@ -174,9 +174,9 @@ function transformEntity({ path, method, base }) {
174
174
  return entity;
175
175
  }
176
176
  word = word.replace(/\.([a-z])/g, (_, p) => p.toUpperCase());
177
- word = import_pluralize.default.singular((0, import_rattail2.pascalCase)(word));
177
+ word = uncountableNouns.includes(word) ? word : import_pluralize.default.singular((0, import_rattail2.pascalCase)(word));
178
178
  if (method === "get" && index === words.length - 1) {
179
- word = import_pluralize.default.plural(word);
179
+ word = uncountableNouns.includes(word) ? `${word}List` : import_pluralize.default.plural(word);
180
180
  }
181
181
  return `${entity}${word}`;
182
182
  }, "");
@@ -258,10 +258,10 @@ __export(generate_exports, {
258
258
  transformPayloads: () => transformPayloads
259
259
  });
260
260
  function transformPayloads(pathItems, options) {
261
- const { transformer, path, base, validateStatus } = options;
261
+ const { transformer, path, base, uncountableNouns, validateStatus } = options;
262
262
  return Object.entries(pathItems).filter(([key]) => SUPPORTED_HTTP_METHODS.includes(key)).reduce((payloads, [method, operation]) => {
263
263
  const url = transformer.url({ path, base });
264
- const args = { path, base, url, method, operation };
264
+ const args = { path, base, url, method, uncountableNouns, operation };
265
265
  const entity = transformer.entity(args);
266
266
  const verb = transformer.verb(args);
267
267
  const requestContentType = operation.requestBody ? getRequestBodyContentType(operation.requestBody) : void 0;
@@ -302,7 +302,7 @@ function transformPayloads(pathItems, options) {
302
302
  }, []);
303
303
  }
304
304
  function partitionApiModules(schema, options) {
305
- const { base, transformer, validateStatus } = options;
305
+ const { base, transformer, uncountableNouns, validateStatus } = options;
306
306
  const schemaPaths = schema.paths ?? {};
307
307
  const schemaPathKeys = base ? Object.keys(schemaPaths).map((key) => key.replace(base, "")) : Object.keys(schemaPaths);
308
308
  const keyToPaths = (0, import_rattail3.groupBy)(schemaPathKeys, (key) => key.split("/")[1]);
@@ -310,7 +310,13 @@ function partitionApiModules(schema, options) {
310
310
  const payloads = paths.reduce((payloads2, path) => {
311
311
  const pathItems = schemaPaths[path];
312
312
  payloads2.push(
313
- ...transformPayloads(pathItems, { ...options, path: base ? base + path : path, transformer, validateStatus })
313
+ ...transformPayloads(pathItems, {
314
+ ...options,
315
+ path: base ? base + path : path,
316
+ transformer,
317
+ uncountableNouns,
318
+ validateStatus
319
+ })
314
320
  );
315
321
  return payloads2;
316
322
  }, []);
@@ -373,7 +379,8 @@ async function generate(userOptions = {}) {
373
379
  output = "./src/apis/generated",
374
380
  typesFilename = "_types.ts",
375
381
  validateStatus = (status) => status >= 200 && status < 300,
376
- transformer = {}
382
+ transformer = {},
383
+ uncountableNouns = []
377
384
  } = options;
378
385
  const mergedTransformer = { ...createTransformer(), ...transformer };
379
386
  const schema = await readSchema(input);
@@ -383,6 +390,7 @@ async function generate(userOptions = {}) {
383
390
  }
384
391
  const apiModules = partitionApiModules(schema, {
385
392
  base,
393
+ uncountableNouns,
386
394
  transformer: mergedTransformer,
387
395
  validateStatus
388
396
  });
package/dist/cli.js CHANGED
@@ -8,7 +8,7 @@ import { Command } from "commander";
8
8
  var program = new Command();
9
9
  program.version(getCliVersion());
10
10
  program.action(async () => {
11
- const { generate } = await import("./generate-5XSJIKDA.js");
11
+ const { generate } = await import("./generate-FIMT7H74.js");
12
12
  return generate();
13
13
  });
14
14
  program.parse();
@@ -4,7 +4,7 @@ import {
4
4
  partitionApiModules,
5
5
  renderApiModules,
6
6
  transformPayloads
7
- } from "./chunk-DJ4JEQRW.js";
7
+ } from "./chunk-MMYZTUXF.js";
8
8
  import "./chunk-KEUG7T6E.js";
9
9
  export {
10
10
  generate,
package/dist/index.cjs CHANGED
@@ -85,7 +85,7 @@ function transformVerb({ method }) {
85
85
  return (0, import_rattail.pascalCase)(method);
86
86
  }
87
87
  }
88
- function transformEntity({ path, method, base }) {
88
+ function transformEntity({ path, method, base, uncountableNouns }) {
89
89
  path = base ? path.replace(base, "") : path;
90
90
  const words = path.split("/").filter(Boolean);
91
91
  return words.reduce((entity, word, index) => {
@@ -93,9 +93,9 @@ function transformEntity({ path, method, base }) {
93
93
  return entity;
94
94
  }
95
95
  word = word.replace(/\.([a-z])/g, (_, p) => p.toUpperCase());
96
- word = import_pluralize.default.singular((0, import_rattail.pascalCase)(word));
96
+ word = uncountableNouns.includes(word) ? word : import_pluralize.default.singular((0, import_rattail.pascalCase)(word));
97
97
  if (method === "get" && index === words.length - 1) {
98
- word = import_pluralize.default.plural(word);
98
+ word = uncountableNouns.includes(word) ? `${word}List` : import_pluralize.default.plural(word);
99
99
  }
100
100
  return `${entity}${word}`;
101
101
  }, "");
@@ -293,10 +293,10 @@ function getResponseMetadataItems(operation, validateStatus) {
293
293
 
294
294
  // src/generate.ts
295
295
  function transformPayloads(pathItems, options) {
296
- const { transformer, path, base, validateStatus } = options;
296
+ const { transformer, path, base, uncountableNouns, validateStatus } = options;
297
297
  return Object.entries(pathItems).filter(([key]) => SUPPORTED_HTTP_METHODS.includes(key)).reduce((payloads, [method, operation]) => {
298
298
  const url = transformer.url({ path, base });
299
- const args = { path, base, url, method, operation };
299
+ const args = { path, base, url, method, uncountableNouns, operation };
300
300
  const entity = transformer.entity(args);
301
301
  const verb = transformer.verb(args);
302
302
  const requestContentType = operation.requestBody ? getRequestBodyContentType(operation.requestBody) : void 0;
@@ -337,7 +337,7 @@ function transformPayloads(pathItems, options) {
337
337
  }, []);
338
338
  }
339
339
  function partitionApiModules(schema, options) {
340
- const { base, transformer, validateStatus } = options;
340
+ const { base, transformer, uncountableNouns, validateStatus } = options;
341
341
  const schemaPaths = schema.paths ?? {};
342
342
  const schemaPathKeys = base ? Object.keys(schemaPaths).map((key) => key.replace(base, "")) : Object.keys(schemaPaths);
343
343
  const keyToPaths = (0, import_rattail3.groupBy)(schemaPathKeys, (key) => key.split("/")[1]);
@@ -345,7 +345,13 @@ function partitionApiModules(schema, options) {
345
345
  const payloads = paths.reduce((payloads2, path) => {
346
346
  const pathItems = schemaPaths[path];
347
347
  payloads2.push(
348
- ...transformPayloads(pathItems, { ...options, path: base ? base + path : path, transformer, validateStatus })
348
+ ...transformPayloads(pathItems, {
349
+ ...options,
350
+ path: base ? base + path : path,
351
+ transformer,
352
+ uncountableNouns,
353
+ validateStatus
354
+ })
349
355
  );
350
356
  return payloads2;
351
357
  }, []);
@@ -408,7 +414,8 @@ async function generate(userOptions = {}) {
408
414
  output = "./src/apis/generated",
409
415
  typesFilename = "_types.ts",
410
416
  validateStatus = (status) => status >= 200 && status < 300,
411
- transformer = {}
417
+ transformer = {},
418
+ uncountableNouns = []
412
419
  } = options;
413
420
  const mergedTransformer = { ...createTransformer(), ...transformer };
414
421
  const schema = await readSchema(input);
@@ -418,6 +425,7 @@ async function generate(userOptions = {}) {
418
425
  }
419
426
  const apiModules = partitionApiModules(schema, {
420
427
  base,
428
+ uncountableNouns,
421
429
  transformer: mergedTransformer,
422
430
  validateStatus
423
431
  });
package/dist/index.d.cts CHANGED
@@ -58,6 +58,7 @@ type TransformerBaseArgs = {
58
58
  url: string;
59
59
  method: string;
60
60
  operation: OperationObject;
61
+ uncountableNouns: string[];
61
62
  };
62
63
  declare function transformModuleName({ name }: {
63
64
  name: string;
@@ -69,7 +70,7 @@ declare function transformUrl({ path, base }: {
69
70
  declare function transformVerb({ method }: {
70
71
  method: string;
71
72
  }): string;
72
- declare function transformEntity({ path, method, base }: TransformerBaseArgs): string;
73
+ declare function transformEntity({ path, method, base, uncountableNouns }: TransformerBaseArgs): string;
73
74
  declare function transformFn({ verb, entity }: {
74
75
  verb: string;
75
76
  entity: string;
@@ -267,16 +268,22 @@ interface GenerateOptions {
267
268
  * The transformer api options, used to override the default transformation rules.
268
269
  */
269
270
  transformer?: Partial<Transformer>;
271
+ /**
272
+ * Certain uncountable nouns that do not change from singular to plural
273
+ */
274
+ uncountableNouns?: string[];
270
275
  }
271
276
  declare function transformPayloads(pathItems: Record<string, OperationObject>, options: {
272
277
  path: string;
273
278
  transformer: Transformer;
274
279
  base: string | undefined;
280
+ uncountableNouns: string[];
275
281
  validateStatus: (status: number) => boolean;
276
282
  }): ApiModulePayload[];
277
283
  declare function partitionApiModules(schema: OpenAPI3, options: {
278
284
  transformer: Transformer;
279
285
  base: string | undefined;
286
+ uncountableNouns: string[];
280
287
  validateStatus: (status: number) => boolean;
281
288
  }): ApiModule[];
282
289
  declare function renderApiModules(apiModules: ApiModule[], options: {
package/dist/index.d.ts CHANGED
@@ -58,6 +58,7 @@ type TransformerBaseArgs = {
58
58
  url: string;
59
59
  method: string;
60
60
  operation: OperationObject;
61
+ uncountableNouns: string[];
61
62
  };
62
63
  declare function transformModuleName({ name }: {
63
64
  name: string;
@@ -69,7 +70,7 @@ declare function transformUrl({ path, base }: {
69
70
  declare function transformVerb({ method }: {
70
71
  method: string;
71
72
  }): string;
72
- declare function transformEntity({ path, method, base }: TransformerBaseArgs): string;
73
+ declare function transformEntity({ path, method, base, uncountableNouns }: TransformerBaseArgs): string;
73
74
  declare function transformFn({ verb, entity }: {
74
75
  verb: string;
75
76
  entity: string;
@@ -267,16 +268,22 @@ interface GenerateOptions {
267
268
  * The transformer api options, used to override the default transformation rules.
268
269
  */
269
270
  transformer?: Partial<Transformer>;
271
+ /**
272
+ * Certain uncountable nouns that do not change from singular to plural
273
+ */
274
+ uncountableNouns?: string[];
270
275
  }
271
276
  declare function transformPayloads(pathItems: Record<string, OperationObject>, options: {
272
277
  path: string;
273
278
  transformer: Transformer;
274
279
  base: string | undefined;
280
+ uncountableNouns: string[];
275
281
  validateStatus: (status: number) => boolean;
276
282
  }): ApiModulePayload[];
277
283
  declare function partitionApiModules(schema: OpenAPI3, options: {
278
284
  transformer: Transformer;
279
285
  base: string | undefined;
286
+ uncountableNouns: string[];
280
287
  validateStatus: (status: number) => boolean;
281
288
  }): ApiModule[];
282
289
  declare function renderApiModules(apiModules: ApiModule[], options: {
package/dist/index.js CHANGED
@@ -20,7 +20,7 @@ import {
20
20
  transformTypeValue,
21
21
  transformUrl,
22
22
  transformVerb
23
- } from "./chunk-DJ4JEQRW.js";
23
+ } from "./chunk-MMYZTUXF.js";
24
24
  import {
25
25
  createStatusCodesByStrategy,
26
26
  findObjectKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api-farmer",
3
- "version": "0.0.23",
3
+ "version": "0.0.24",
4
4
  "description": "API module generation tool based on Openapi3/Swagger2.",
5
5
  "keywords": [
6
6
  "cli",