@moccona/apicodegen 0.0.3 → 0.0.7

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/npm/index.d.ts DELETED
@@ -1,536 +0,0 @@
1
- import * as typescript from 'typescript';
2
- import { Statement, Node, ParameterDeclaration, TypeNode, Block } from 'typescript';
3
- import { OpenAPIV3 } from 'openapi-types';
4
- import { ServerOptions, PluginOption } from 'vite';
5
-
6
- /**
7
- * Simple represenration for JSON object
8
- */
9
- type JSONValue = {
10
- [K: string]: string | number | boolean | JSONValue | (string | number | boolean | JSONValue)[];
11
- };
12
- declare enum SchemaType {
13
- schemas = "schemas",
14
- parameters = "parameters",
15
- responses = "responses",
16
- requestBodies = "requestBodies"
17
- }
18
- declare enum NonArraySchemaType {
19
- "object" = "object",
20
- "string" = "string",
21
- "number" = "number",
22
- "boolean" = "boolean",
23
- "integer" = "integer",
24
- "enum" = "enum",
25
- "file" = "file"
26
- }
27
- declare enum ArraySchemaType {
28
- "array" = "array"
29
- }
30
- declare enum SchemaFormatType {
31
- "string" = "string",
32
- "number" = "number",
33
- "boolean" = "boolean",
34
- "file" = "file",
35
- "binary" = "binary",
36
- "blob" = "blob"
37
- }
38
- declare enum ParameterIn {
39
- "header" = "header",
40
- "body" = "body",
41
- "query" = "query",
42
- "cookie" = "cookie",
43
- "path" = "path",
44
- "formData" = "formData"
45
- }
46
- interface ReferenceObject {
47
- $ref: string;
48
- }
49
- interface EnumSchemaObject {
50
- name: string;
51
- enum: (string | number)[];
52
- }
53
- interface SingleTypeSchemaObject {
54
- type: keyof typeof NonArraySchemaType | string;
55
- description?: string;
56
- allOf?: SchemaObject[];
57
- anyOf?: SchemaObject[];
58
- deprecated?: boolean;
59
- enum?: (string | number)[];
60
- format?: keyof typeof SchemaFormatType;
61
- oneOf?: SchemaObject[];
62
- properties?: Record<string, SchemaObject>;
63
- readonly?: boolean;
64
- required?: string[] | boolean;
65
- ref?: string;
66
- }
67
- interface ArrayTypeSchemaObject {
68
- type: keyof typeof ArraySchemaType;
69
- items?: SchemaObject;
70
- required?: boolean;
71
- description?: string;
72
- ref?: string;
73
- }
74
- type SchemaObject = SingleTypeSchemaObject | ArrayTypeSchemaObject;
75
- type ParameterObject = {
76
- name: string;
77
- in: keyof typeof ParameterIn;
78
- schema?: SchemaObject;
79
- required?: boolean;
80
- description?: string;
81
- deprecated?: boolean;
82
- ref?: string;
83
- };
84
- declare enum MediaTypes {
85
- JSON = "application/json",
86
- TEXT = "text",
87
- IMAGE = "image",
88
- AUDIO = "audio",
89
- VIDEO = "video"
90
- }
91
- type MediaTypeObject = {
92
- type: MediaTypes | keyof typeof MediaTypes;
93
- schema?: SchemaObject;
94
- };
95
- type ResponsesObject = Record<string, MediaTypeObject[]>;
96
- type RequestBodyObject = ResponsesObject;
97
- declare enum HttpMethods {
98
- GET = "get",
99
- PUT = "put",
100
- POST = "post",
101
- DELETE = "delete",
102
- OPTIONS = "options",
103
- HEAD = "head",
104
- PATCH = "patch",
105
- TRACE = "trace"
106
- }
107
- type OperationObject = {
108
- method: string;
109
- summary?: string;
110
- description?: string;
111
- operationId?: string;
112
- externalDocs?: {
113
- url: string;
114
- description?: string;
115
- }[];
116
- parameters?: ParameterObject[];
117
- requestBody?: MediaTypeObject[];
118
- responses: MediaTypeObject[];
119
- deprecated?: boolean;
120
- };
121
- type PathObject = {
122
- ref?: string;
123
- summary?: string;
124
- description?: string;
125
- parameters?: ParameterObject[];
126
- } & Partial<Record<HttpMethods, OperationObject>>;
127
- type PathsObject = Record<string, OperationObject[]>;
128
- type FetchDocRequestInit = {
129
- method?: string;
130
- body?: string | FormData;
131
- headers?: Record<string, string>;
132
- };
133
- declare enum Adaptors {
134
- fetch = "fetch",
135
- axios = "axios"
136
- }
137
- type ProviderInitOptions = {
138
- docURL: string;
139
- output: string;
140
- baseURL?: string;
141
- importClientSource?: string;
142
- requestOptions?: FetchDocRequestInit;
143
- verbose?: boolean;
144
- adaptor?: keyof typeof Adaptors;
145
- };
146
- interface ProviderInitResult {
147
- readonly enums: EnumSchemaObject[];
148
- readonly schemas: Record<string, SchemaObject>;
149
- readonly parameters: Record<string, ParameterObject>;
150
- readonly responses: Record<string, ResponsesObject>;
151
- readonly requestBodies: Record<string, RequestBodyObject>;
152
- readonly apis: PathsObject;
153
- }
154
-
155
- /**
156
- * @file Adapter abstract class definition
157
- * @author wp.l
158
- * @description Base adapter implementation for various code generation tools
159
- */
160
-
161
- /**
162
- * Base adapter for tool
163
- * This abstract class serves as the foundation for implementing adapters for different code generation tools
164
- */
165
- declare abstract class Adapter {
166
- /**
167
- * @abstract The unique name/identifier for this adapter implementation
168
- */
169
- abstract readonly name: string;
170
- /**
171
- * @abstract The name of the field used to specify the HTTP method in API calls
172
- */
173
- abstract readonly methodFieldName: string;
174
- /**
175
- * @abstract The name of the field used to specify the request body in API calls
176
- */
177
- abstract readonly bodyFieldName: string;
178
- /**
179
- * @abstract The name of the field used to specify request headers in API calls
180
- */
181
- abstract readonly headersFieldName: string;
182
- /**
183
- * @abstract The name of the field used to specify query parameters in API calls
184
- */
185
- abstract readonly queryFieldName: string;
186
- /**
187
- * @abstract
188
- * @param {string} uri - The API endpoint URI
189
- * @param {string} method - The HTTP method (e.g., GET, POST, etc.)
190
- * @param {ParameterObject[]} parameters - An array of parameters for the API call
191
- * @param {MediaTypeObject | undefined} requestBody - The request body payload (if applicable)
192
- * @param {MediaTypeObject | undefined} response - The expected response format (if applicable)
193
- * @param {Adapter} adapter - An instance of the adapter being used
194
- * @param {boolean} useFormData - Flag indicating whether to use FormData for the request body
195
- * @param {boolean} useJSONResponse - Flag indicating whether the response should be parsed as JSON
196
- * @returns {Statement[]} An array of TypeScript AST statements representing the generated code
197
- */
198
- abstract client(uri: string, method: string, parameters: ParameterObject[], requestBody: MediaTypeObject | undefined, response: MediaTypeObject | undefined, adapter: Adapter, useFormData: boolean, useJSONResponse: boolean): Statement[];
199
- }
200
-
201
- /**
202
- * @file Base class implementation
203
- * @author wp.l
204
- * @description Base utility class providing common methods for code generation and API handling
205
- */
206
-
207
- /**
208
- * Represents success HTTP status codes.
209
- * Each key is a string representation of a success HTTP status code.
210
- */
211
- declare const SuccessHttpStatusCode: {
212
- "200": string;
213
- "201": string;
214
- "202": string;
215
- "203": string;
216
- "204": string;
217
- "205": string;
218
- "206": string;
219
- "207": string;
220
- "208": string;
221
- "226": string;
222
- };
223
- /**
224
- * Base abstract class providing common utility methods.
225
- */
226
- declare abstract class Base {
227
- protected constructor();
228
- /**
229
- * Converts a reference string to a meaningful name.
230
- * @param ref - The reference string to process.
231
- * @param [doc] - Optional document reference for context.
232
- * @returns - The processed name.
233
- */
234
- static ref2name(ref: string, doc?: any): string;
235
- /**
236
- * Converts an API path to a function name.
237
- * @param path - The API endpoint path.
238
- * @param [method] - The HTTP method (e.g., GET, POST).
239
- * @param [operationId] - Unique identifier for the operation.
240
- * @returns - The generated function name.
241
- */
242
- static pathToFnName(path: string, method?: string, _operationId?: string): string;
243
- /**
244
- * Normalizes a string by replacing special characters and avoiding TypeScript keywords.
245
- * @param text - Input text to normalize.
246
- * @returns - The normalized string.
247
- */
248
- static normalize(text: string): string;
249
- /**
250
- * Capitalizes the first character of a string.
251
- * @param text - Input string.
252
- * @returns - Capitalized string.
253
- */
254
- static capitalize(text: string): string;
255
- /**
256
- * Converts a string to camelCase.
257
- * @param text - Input string.
258
- * @returns - CamelCase string.
259
- */
260
- static camelCase(text: string): string;
261
- /**
262
- * Converts a string to UpperCamelCase.
263
- * @param text - Input string.
264
- * @returns - UpperCamelCase string.
265
- */
266
- static upperCamelCase(text: string): string;
267
- /**
268
- * Fetches documentation from a given URL.
269
- * @param url - The URL to fetch the documentation from.
270
- * @param requestInit - Additional request parameters.
271
- * @returns - A promise resolving to the fetched documentation data.
272
- */
273
- static fetchDoc<T = unknown>(url: string, requestInit?: FetchDocRequestInit): Promise<T>;
274
- /**
275
- * Determines the media type from a given media type string.
276
- * @param mediaType - The media type string to evaluate.
277
- * @returns - The matched MediaTypes or null.
278
- */
279
- static getMediaType(mediaType: string): MediaTypes | undefined;
280
- /**
281
- * Checks if a schema is a valid enum type that isn't boolean.
282
- * @param a - The schema object to evaluate.
283
- * @returns - True if the schema is a valid non-boolean enum.
284
- */
285
- static isValidEnumType(a: SchemaObject): boolean;
286
- /**
287
- * Checks if a schema represents a boolean enum.
288
- * @param a - The schema object to evaluate.
289
- * @returns - True if the schema is a boolean enum.
290
- */
291
- static isBooleanEnum(a: SchemaObject): boolean;
292
- /**
293
- * Checks if two enum schemas are identical.
294
- * @param a - First enum schema to compare.
295
- * @param b - Second enum schema to compare.
296
- * @returns - True if the enums are identical.
297
- */
298
- private static isSameEnum;
299
- /**
300
- * Filters out duplicate enum schemas from an array.
301
- * @param enums - Array of enum schemas to process.
302
- * @returns - Array of unique enum schemas.
303
- */
304
- static uniqueEnums(enums: EnumSchemaObject[]): EnumSchemaObject[];
305
- /**
306
- * Finds the first occurrence of a matching enum schema in an array.
307
- * @param a - The enum schema to find.
308
- * @param enums - Array of enum schemas to search.
309
- * @returns - The found schema or undefined.
310
- */
311
- static findSameSchema(a: EnumSchemaObject, enums: EnumSchemaObject[]): EnumSchemaObject | undefined;
312
- /**
313
- * Checks if an object is a reference object.
314
- * @param schema - The object to check.
315
- * @returns - True if the object is a reference.
316
- */
317
- static isRef(schema: unknown): schema is ReferenceObject;
318
- }
319
-
320
- /**
321
- * @file Base class implementation
322
- * @author wp.l
323
- * @description This file defines the `Provider` abstract class, which serves as a base for providers responsible for parsing and processing API documentation.
324
- */
325
-
326
- /**
327
- * Abstract Provider Class.
328
- *
329
- * The Provider class is designed to be extended by specific implementations (e.g., OpenAPI 2 provider, OpenAPI 3 provider).
330
- * It handles the initialization of the provider and the parsing of documentation into structured data.
331
- *
332
- * @example
333
- *
334
- * ```ts
335
- * /// Example of how this class might be used by a subclass:
336
- * class OpenAPIProvider extends Provider {
337
- * /// Implement the parse method to handle OpenAPI-specific documentation parsing.
338
- * parse(doc: unknown): ProviderInitResult {
339
- * /// Implementation details...
340
- * }
341
- * }
342
- *
343
- * /// Initializing a provider with configuration and documentation data:
344
- * const initOptions: ProviderInitOptions = {
345
- * docURL: "https://example.com/api/swagger.json",
346
- * baseURL: "https://api.example.com",
347
- * output: "./generated",
348
- * requestOptions: {
349
- * headers: { "Content-Type": "application/json" },
350
- * },
351
- * importClientSource: "generated/client",
352
- * };
353
- *
354
- * const docData = fetchSwaggerDoc();
355
- * const provider = new OpenAPIProvider(initOptions, docData);
356
- * ```
357
- */
358
- declare abstract class Provider implements ProviderInitResult, ProviderInitOptions {
359
- /** collection of enum schemas */
360
- readonly enums: EnumSchemaObject[];
361
- /** collection of schemas indexed by name */
362
- readonly schemas: Record<string, SchemaObject>;
363
- /** collection of parameters indexed by name */
364
- readonly parameters: Record<string, ParameterObject>;
365
- /** collection of API responses indexed by name */
366
- readonly responses: Record<string, ResponsesObject>;
367
- /** collection of request bodies indexed by name */
368
- readonly requestBodies: Record<string, RequestBodyObject>;
369
- /** collection of API endpoints (operations) indexed by path */
370
- readonly apis: Record<string, OperationObject[]>;
371
- /** URL for fetching API documentation */
372
- readonly docURL: string;
373
- /** base URL for API endpoints */
374
- readonly baseURL: string;
375
- /** output directory for generated code */
376
- readonly output: string;
377
- /** request options for API documentation fetch */
378
- readonly requestOptions: FetchDocRequestInit;
379
- /** source path for imported client */
380
- readonly importClientSource: string;
381
- /**
382
- * Provider Constructor.
383
- * @param {ProviderInitOptions} initOptions - Initial configuration for the provider.
384
- * @param {unknown} doc - Raw API documentation data to be parsed.
385
- */
386
- constructor(initOptions: ProviderInitOptions, doc: unknown);
387
- /**
388
- * Abstract Parse Method.
389
- * @abstract
390
- * @param {unknown} doc - Raw API documentation data.
391
- * @returns {ProviderInitResult} - Parsed documentation data.
392
- *
393
- * This method must be implemented by subclasses to parse the raw documentation into structured data.
394
- */
395
- abstract parse(doc: unknown): ProviderInitResult;
396
- }
397
-
398
- /**
399
- * File containing the implementation of the AxiosAdapter class.
400
- * This adapter is responsible for generating code that uses the Axios HTTP client library.
401
- */
402
-
403
- /**
404
- * Adapter class implementing support for generating code that makes use of the Axios HTTP client library.
405
- * This class defines custom behavior and field mappings specific to the Axios client.
406
- */
407
- declare class AxiosAdapter extends Adapter {
408
- /**
409
- * Name of the field used to specify the HTTP method in the request configuration.
410
- */
411
- readonly methodFieldName = "method";
412
- /**
413
- * Name of the field used to specify the request body (data) in the request configuration.
414
- */
415
- readonly bodyFieldName = "data";
416
- /**
417
- * Name of the field used to specify the request headers in the request configuration.
418
- */
419
- readonly headersFieldName = "headers";
420
- /**
421
- * Name of the field used to specify the query parameters in the request configuration.
422
- */
423
- readonly queryFieldName = "params";
424
- /**
425
- * The name of the client this adapter is configured for, which is 'axios' in this case.
426
- */
427
- readonly name = "axios";
428
- /**
429
- * Method that should generate and return the client-specific configuration statements.
430
- *
431
- * @returns {Statement[]} An array of TypeScript statements that define the client configuration.
432
- *
433
- * @throws {Error} Indicates that the method is not yet implemented and needs to be filled in.
434
- */
435
- client(uri: string, method: string, parameters: ParameterObject[], requestBody: MediaTypeObject | undefined, response: MediaTypeObject | undefined, adapter: Adapter, shouldUseFormData: boolean): Statement[];
436
- }
437
-
438
- /**
439
- * FetchAdapter is an adapter class that generates client-side fetch requests.
440
- * It handles parameters, headers, and request bodies to construct proper fetch calls.
441
- */
442
- declare class FetchAdapter extends Adapter {
443
- readonly methodFieldName = "method";
444
- readonly bodyFieldName = "body";
445
- readonly headersFieldName = "headers";
446
- readonly queryFieldName = "";
447
- readonly name = "fetch";
448
- /**
449
- * Generates client code for making API requests using the Fetch API.
450
- * @param uri - The API endpoint URI
451
- * @param method - The HTTP method (GET, POST, etc.)
452
- * @param parameters - Array of parameters to include in the request
453
- * @param requestBody - The request body media type definition
454
- * @param response - The response media type definition
455
- * @param adapter - The adapter instance
456
- * @param shouldUseFormData - Flag to use FormData for the request body
457
- * @param shouldUseJSONResponse - Flag to use JSON parsing for the response
458
- * @return - An array of generated TypeScript statements
459
- */
460
- client(uri: string, method: string, parameters: ParameterObject[], requestBody: MediaTypeObject | undefined, response: MediaTypeObject | undefined, adapter: Adapter, shouldUseFormData: boolean, shouldUseJSONResponse: boolean): Statement[];
461
- }
462
-
463
- /**
464
- * Represents a comment object with optional tag and message.
465
- */
466
- type CommentObject = {
467
- tag?: string;
468
- comment: string;
469
- };
470
- /**
471
- * Array of comment objects to be added to the code.
472
- */
473
- type Comments = CommentObject[];
474
- declare class Generator {
475
- /**
476
- * Converts an array of TypeScript statements into a formatted string of code.
477
- *
478
- * @param statements - The array of TypeScript statement nodes.
479
- * @returns Formatted code as a string.
480
- * @throws {Error} If no valid statements are provided.
481
- */
482
- static toCode(statements: Statement[]): string;
483
- static write(code: string, filepath: string): Promise<void>;
484
- /**
485
- * Converts a path string with parameters into a TypeScript template expression.
486
- * Handles query parameters and path placeholders.
487
- *
488
- * @param path - The base path string containing placeholders.
489
- * @param parameters - Array of parameter objects defining the parameters.
490
- * @param basePath - Optional base path to prepend (default: "").
491
- * @returns A TypeScript template expressi
492
- */
493
- static toUrlTemplate(path: string, parameters: ParameterObject[], basePath?: string): typescript.NoSubstitutionTemplateLiteral | typescript.TemplateExpression;
494
- /**
495
- * Adds synthetic comments to a TypeScript AST node.
496
- *
497
- * @param node - The target AST node.
498
- * @param comments - Array of comment objects to add.
499
- */
500
- static addComments(node: Node, comments: Comments): void;
501
- /**
502
- * Checks if a schema represents a binary type.
503
- *
504
- * @param schema - The schema object to check.
505
- * @returns true if the schema is a binary type, false otherwise.
506
- */
507
- static isBinarySchema(schema: SchemaObject): boolean;
508
- static toRequestBodyTypeNode(schema: SchemaObject): ParameterDeclaration;
509
- static toTypeNode(schema: SchemaObject): TypeNode;
510
- static toDeclarationNodes(parameters: ParameterObject[]): ParameterDeclaration[];
511
- static toFormDataStatement(parameters: ParameterObject[], requestBody?: SchemaObject): Statement[];
512
- static bodyBlock(uri: string, method: string, parameters: ParameterObject[], requestBody: MediaTypeObject | undefined, response: MediaTypeObject | undefined, adapter: Adapter): Block;
513
- static schemaToStatemets(parsedDoc: ProviderInitResult, adaptor: Adapter, options: Omit<ProviderInitOptions, "docURL" | "output" | "requestOptions">): Statement[];
514
- static prettier(code: string): Promise<string>;
515
- static genCode(schema: ProviderInitResult, initOptions: ProviderInitOptions, adaptor: Adapter): Promise<string>;
516
- }
517
-
518
- declare enum OpenAPIVersion {
519
- v2 = "v2",
520
- v3 = "v3",
521
- v3_1 = "v3_1",
522
- unknown = "unknown"
523
- }
524
- declare class OpenAPIProvider extends Provider {
525
- parse(doc: OpenAPIV3.Document): ProviderInitResult;
526
- }
527
- declare function codeGen(initOptions: ProviderInitOptions): Promise<string>;
528
-
529
- type apiCodeGenPluginOptions = ProviderInitOptions & {
530
- name: string;
531
- proxy?: ServerOptions["proxy"];
532
- };
533
- declare const tsc: (path: string) => Promise<void>;
534
- declare function apiCodeGenPlugin(options: apiCodeGenPluginOptions[]): PluginOption;
535
-
536
- export { Adapter, Adaptors, ArraySchemaType, type ArrayTypeSchemaObject, AxiosAdapter, Base, type CommentObject, type Comments, type EnumSchemaObject, FetchAdapter, type FetchDocRequestInit, Generator, HttpMethods, type JSONValue, type MediaTypeObject, MediaTypes, NonArraySchemaType, OpenAPIProvider, OpenAPIVersion, type OperationObject, ParameterIn, type ParameterObject, type PathObject, type PathsObject, Provider, type ProviderInitOptions, type ProviderInitResult, type ReferenceObject, type RequestBodyObject, type ResponsesObject, SchemaFormatType, type SchemaObject, SchemaType, type SingleTypeSchemaObject, SuccessHttpStatusCode, apiCodeGenPlugin, type apiCodeGenPluginOptions, codeGen, tsc };