@sdk-it/typescript 0.37.1 → 0.39.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/connect.d.ts +1 -0
- package/dist/connect.d.ts.map +1 -0
- package/dist/global.d.js +1 -0
- package/dist/global.d.js.map +7 -0
- package/dist/index.js +102 -541
- package/dist/index.js.map +2 -2
- package/dist/lib/client.d.ts +1 -2
- package/dist/lib/client.d.ts.map +1 -1
- package/dist/lib/connect.d.ts +162 -0
- package/dist/lib/connect.d.ts.map +1 -0
- package/dist/lib/generate.d.ts.map +1 -1
- package/dist/lib/generator.d.ts.map +1 -1
- package/dist/lib/sdk.d.ts +1 -5
- package/dist/lib/sdk.d.ts.map +1 -1
- package/dist/lib/style.d.ts +0 -3
- package/dist/lib/style.d.ts.map +1 -1
- package/dist/src/index.js +5 -0
- package/dist/src/index.js.map +7 -0
- package/dist/src/lib/agent/ai-sdk.js +60 -0
- package/dist/src/lib/agent/ai-sdk.js.map +7 -0
- package/dist/src/lib/agent/openai-agents.js +42 -0
- package/dist/src/lib/agent/openai-agents.js.map +7 -0
- package/dist/src/lib/client.js +152 -0
- package/dist/src/lib/client.js.map +7 -0
- package/dist/src/lib/emitters/interface.js +169 -0
- package/dist/src/lib/emitters/interface.js.map +7 -0
- package/dist/src/lib/emitters/snippet.js +191 -0
- package/dist/src/lib/emitters/snippet.js.map +7 -0
- package/dist/src/lib/emitters/zod.js +271 -0
- package/dist/src/lib/emitters/zod.js.map +7 -0
- package/dist/src/lib/generate.js +382 -0
- package/dist/src/lib/generate.js.map +7 -0
- package/dist/src/lib/generator.js +268 -0
- package/dist/src/lib/generator.js.map +7 -0
- package/dist/src/lib/import-utilities.js +56 -0
- package/dist/src/lib/import-utilities.js.map +7 -0
- package/dist/src/lib/options.js +3 -0
- package/dist/src/lib/options.js.map +7 -0
- package/dist/src/lib/readme/prop.emitter.js +283 -0
- package/dist/src/lib/readme/prop.emitter.js.map +7 -0
- package/dist/src/lib/readme/readme.js +105 -0
- package/dist/src/lib/readme/readme.js.map +7 -0
- package/dist/src/lib/sdk.js +236 -0
- package/dist/src/lib/sdk.js.map +7 -0
- package/dist/src/lib/status-map.js +28 -0
- package/dist/src/lib/status-map.js.map +7 -0
- package/dist/src/lib/style.js +1 -0
- package/dist/src/lib/style.js.map +7 -0
- package/dist/src/lib/typescript-snippet.js +738 -0
- package/dist/src/lib/typescript-snippet.js.map +7 -0
- package/package.json +4 -4
package/dist/lib/client.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { Spec } from './sdk.ts';
|
|
2
|
-
|
|
3
|
-
declare const _default: (spec: Omit<Spec, "operations">, style: Style) => string;
|
|
2
|
+
declare const _default: (spec: Omit<Spec, "operations">) => string;
|
|
4
3
|
export default _default;
|
|
5
4
|
//# sourceMappingURL=client.d.ts.map
|
package/dist/lib/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/lib/client.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/lib/client.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;yBAErB,MAAM,IAAI,CAAC,IAAI,EAAE,YAAY,CAAC;AAA9C,wBA8KE"}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { OpenAPIObject } from 'openapi3-ts/oas31';
|
|
2
|
+
import type { InjectImport, NaunceResponseAnalyzer, OnOperation, ResponseAnalyzerFn } from '@sdk-it/core';
|
|
3
|
+
import type { TypeScriptGeneratorOptions } from './options.ts';
|
|
4
|
+
/**
|
|
5
|
+
* Options for the connect function
|
|
6
|
+
*/
|
|
7
|
+
export interface ConnectOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Path to the backend project's tsconfig.json
|
|
10
|
+
* This tsconfig should include all source files that contain route definitions
|
|
11
|
+
*/
|
|
12
|
+
tsconfigPath: string;
|
|
13
|
+
/**
|
|
14
|
+
* Framework-specific response analyzer
|
|
15
|
+
*
|
|
16
|
+
* Use the appropriate analyzer for your backend framework:
|
|
17
|
+
* - `@sdk-it/generic` - Generic analyzer (default, works with most frameworks)
|
|
18
|
+
* - `@sdk-it/hono` - Hono framework specific analyzer
|
|
19
|
+
*
|
|
20
|
+
* Or provide a custom analyzer function
|
|
21
|
+
*
|
|
22
|
+
* @default responseAnalyzer from @sdk-it/generic
|
|
23
|
+
*/
|
|
24
|
+
responseAnalyzer?: ResponseAnalyzerFn | NaunceResponseAnalyzer;
|
|
25
|
+
/**
|
|
26
|
+
* Custom type mappings for OpenAPI generation
|
|
27
|
+
* Maps TypeScript types to OpenAPI schema types
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* typesMap: {
|
|
32
|
+
* 'Date': 'string',
|
|
33
|
+
* 'Buffer': 'string'
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
typesMap?: Record<string, string>;
|
|
38
|
+
/**
|
|
39
|
+
* Name for the generated client class
|
|
40
|
+
*
|
|
41
|
+
* @default 'APIClient'
|
|
42
|
+
*/
|
|
43
|
+
clientName?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Custom output directory for generated files
|
|
46
|
+
* If not specified, generates to node_modules/.sdk-it/[hash]
|
|
47
|
+
*
|
|
48
|
+
* The hash is derived from the tsconfigPath to ensure stable location
|
|
49
|
+
* across regenerations while keeping files hidden from the user
|
|
50
|
+
*/
|
|
51
|
+
outputDir?: string;
|
|
52
|
+
/**
|
|
53
|
+
* Additional imports to inject before resolving Zod schemas
|
|
54
|
+
* Useful when your validation schemas reference external types or utilities
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* imports: [
|
|
59
|
+
* { module: '../types', named: ['CustomType'] },
|
|
60
|
+
* { module: 'zod', named: ['z'] }
|
|
61
|
+
* ]
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
imports?: InjectImport[];
|
|
65
|
+
/**
|
|
66
|
+
* Custom operation handler
|
|
67
|
+
* Called for each detected API operation, allowing you to customize
|
|
68
|
+
* the generated OpenAPI operation object
|
|
69
|
+
*/
|
|
70
|
+
onOperation?: OnOperation;
|
|
71
|
+
/**
|
|
72
|
+
* OpenAPI spec metadata
|
|
73
|
+
*/
|
|
74
|
+
info?: {
|
|
75
|
+
/**
|
|
76
|
+
* API title
|
|
77
|
+
* @default clientName or 'API Client'
|
|
78
|
+
*/
|
|
79
|
+
title?: string;
|
|
80
|
+
/**
|
|
81
|
+
* API version
|
|
82
|
+
* @default '1.0.0'
|
|
83
|
+
*/
|
|
84
|
+
version?: string;
|
|
85
|
+
/**
|
|
86
|
+
* API description
|
|
87
|
+
*/
|
|
88
|
+
description?: string;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Generation options (advanced)
|
|
92
|
+
* Allows fine-grained control over SDK generation
|
|
93
|
+
*/
|
|
94
|
+
generatorOptions?: Partial<Pick<TypeScriptGeneratorOptions, 'useTsExtension' | 'pagination' | 'style' | 'formatCode' | 'cleanup'>>;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Result of the connect function
|
|
98
|
+
*/
|
|
99
|
+
export interface ConnectResult {
|
|
100
|
+
/**
|
|
101
|
+
* Absolute path to the generated client.ts file
|
|
102
|
+
* Import this to use the API client
|
|
103
|
+
*/
|
|
104
|
+
clientPath: string;
|
|
105
|
+
/**
|
|
106
|
+
* Absolute path to the generated index.ts file
|
|
107
|
+
* Barrel export file that re-exports all public APIs
|
|
108
|
+
*/
|
|
109
|
+
indexPath: string;
|
|
110
|
+
/**
|
|
111
|
+
* Absolute path to the output directory
|
|
112
|
+
* Contains all generated SDK files
|
|
113
|
+
*/
|
|
114
|
+
outputDir: string;
|
|
115
|
+
/**
|
|
116
|
+
* The generated OpenAPI specification
|
|
117
|
+
* Useful for debugging or further processing
|
|
118
|
+
*/
|
|
119
|
+
openapi: OpenAPIObject;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Connect to a backend TypeScript project and generate a typed SDK client
|
|
123
|
+
*
|
|
124
|
+
* This function:
|
|
125
|
+
* 1. Analyzes your backend TypeScript code to extract API routes and types
|
|
126
|
+
* 2. Generates an OpenAPI specification from the analyzed code
|
|
127
|
+
* 3. Generates a fully-typed TypeScript SDK client
|
|
128
|
+
* 4. Outputs the SDK to node_modules/.sdk-it/[hash] (hidden from user)
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```ts
|
|
132
|
+
* import { connect } from '@sdk-it/typescript/connect';
|
|
133
|
+
*
|
|
134
|
+
* // Generate SDK from backend project
|
|
135
|
+
* const result = await connect({
|
|
136
|
+
* tsconfigPath: '../backend/tsconfig.json',
|
|
137
|
+
* clientName: 'MyAPIClient',
|
|
138
|
+
* });
|
|
139
|
+
*
|
|
140
|
+
* // Import and use the generated client
|
|
141
|
+
* const { MyAPIClient } = await import(result.indexPath);
|
|
142
|
+
* const client = new MyAPIClient({ baseUrl: 'http://localhost:3000' });
|
|
143
|
+
* const data = await client.users.getUser({ userId: '123' });
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* @example With Hono framework
|
|
147
|
+
* ```ts
|
|
148
|
+
* import { connect } from '@sdk-it/typescript/connect';
|
|
149
|
+
* import { responseAnalyzer } from '@sdk-it/hono';
|
|
150
|
+
*
|
|
151
|
+
* const result = await connect({
|
|
152
|
+
* tsconfigPath: '../api/tsconfig.json',
|
|
153
|
+
* responseAnalyzer, // Use Hono-specific analyzer
|
|
154
|
+
* clientName: 'HonoAPIClient',
|
|
155
|
+
* });
|
|
156
|
+
* ```
|
|
157
|
+
*
|
|
158
|
+
* @param options - Configuration options for SDK generation
|
|
159
|
+
* @returns Promise resolving to paths and metadata of generated SDK
|
|
160
|
+
*/
|
|
161
|
+
export declare function connect(options: ConnectOptions): Promise<ConnectResult>;
|
|
162
|
+
//# sourceMappingURL=connect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../../src/lib/connect.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,KAAK,EACV,YAAY,EACZ,sBAAsB,EACtB,WAAW,EACX,kBAAkB,EACnB,MAAM,cAAc,CAAC;AAQtB,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAE/D;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,YAAY,EAAE,MAAM,CAAC;IAErB;;;;;;;;;;OAUG;IACH,gBAAgB,CAAC,EAAE,kBAAkB,GAAG,sBAAsB,CAAC;IAE/D;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAElC;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,EAAE,YAAY,EAAE,CAAC;IAEzB;;;;OAIG;IACH,WAAW,CAAC,EAAE,WAAW,CAAC;IAE1B;;OAEG;IACH,IAAI,CAAC,EAAE;QACL;;;WAGG;QACH,KAAK,CAAC,EAAE,MAAM,CAAC;QAEf;;;WAGG;QACH,OAAO,CAAC,EAAE,MAAM,CAAC;QAEjB;;WAEG;QACH,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB,CAAC;IAEF;;;OAGG;IACH,gBAAgB,CAAC,EAAE,OAAO,CACxB,IAAI,CACF,0BAA0B,EAC1B,gBAAgB,GAAG,YAAY,GAAG,OAAO,GAAG,YAAY,GAAG,SAAS,CACrE,CACF,CAAC;CACH;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;;OAGG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,OAAO,EAAE,aAAa,CAAC;CACxB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAsB,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC,CA0D7E"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../../src/lib/generate.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AA+BvD,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAK/D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAkC1C,wBAAsB,QAAQ,CAC5B,OAAO,EAAE,aAAa,EACtB,QAAQ,EAAE,0BAA0B,
|
|
1
|
+
{"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../../src/lib/generate.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AA+BvD,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAK/D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAkC1C,wBAAsB,QAAQ,CAC5B,OAAO,EAAE,aAAa,EACtB,QAAQ,EAAE,0BAA0B,iBAqPrC;AA+CD,wBAAgB,QAAQ,CACtB,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,CAAC,EAC1C,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EAC9B,UAAU,EAAE,YAAY,OAsDzB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../src/lib/generator.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAIV,YAAY,EACb,MAAM,mBAAmB,CAAC;AAI3B,OAAO,EACL,KAAK,EAAE,EACP,KAAK,oBAAoB,EAE1B,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,KAAK,cAAc,EAAyB,MAAM,UAAU,CAAC;AACtE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAwCxC,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,OAAO,CAAC;CACrB;AACD,MAAM,WAAW,MAAM;IACrB,UAAU,EAAE,OAAO,CAAC;IACpB,eAAe,EAAE,MAAM,CAAC;IACxB,aAAa,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,YAAY,EAAE,WAAW,EAAE,CAAC;IAC5B,eAAe,EAAE,MAAM,GAAG,SAAS,CAAC;CACrC;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE;IACnC;;;OAGG;IACH,IAAI,EAAE,EAAE,CAAC;IACT,KAAK,EAAE,KAAK,CAAC;IACb,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,CAAC;CACxC;;;;
|
|
1
|
+
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../src/lib/generator.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAIV,YAAY,EACb,MAAM,mBAAmB,CAAC;AAI3B,OAAO,EACL,KAAK,EAAE,EACP,KAAK,oBAAoB,EAE1B,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,KAAK,cAAc,EAAyB,MAAM,UAAU,CAAC;AACtE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAwCxC,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,OAAO,CAAC;CACrB;AACD,MAAM,WAAW,MAAM;IACrB,UAAU,EAAE,OAAO,CAAC;IACpB,eAAe,EAAE,MAAM,CAAC;IACxB,aAAa,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,YAAY,EAAE,WAAW,EAAE,CAAC;IAC5B,eAAe,EAAE,MAAM,GAAG,SAAS,CAAC;CACrC;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE;IACnC;;;OAGG;IACH,IAAI,EAAE,EAAE,CAAC;IACT,KAAK,EAAE,KAAK,CAAC;IACb,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,CAAC;CACxC;;;;EA8GA;AA2ED,wBAAgB,UAAU,CAAC,IAAI,EAAE,EAAE,EAAE,SAAS,EAAE,oBAAoB;;;;EA8BnE;AAED,wBAAgB,eAAe,CAC7B,EAAE,EAAE,EAAE,EACN,SAAS,EAAE,oBAAoB,EAC/B,IAAI,EAAE,MAAM;;;EA0Bb"}
|
package/dist/lib/sdk.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { ResponseObject } from 'openapi3-ts/oas31';
|
|
2
2
|
import { type IR, type OurParameter, type TunedOperationObject } from '@sdk-it/spec';
|
|
3
3
|
import { type MakeImportFn } from './import-utilities.ts';
|
|
4
|
-
import type { Style } from './style.ts';
|
|
5
4
|
export type Parser = 'chunked' | 'buffered';
|
|
6
5
|
export interface SdkConfig {
|
|
7
6
|
/**
|
|
@@ -33,10 +32,7 @@ export interface Operation {
|
|
|
33
32
|
inputs: Record<string, OperationInput>;
|
|
34
33
|
outgoingContentType?: string;
|
|
35
34
|
}
|
|
36
|
-
export declare function toEndpoint(groupName: string, spec: IR, specOperation: TunedOperationObject, operation: Operation
|
|
37
|
-
makeImport: MakeImportFn;
|
|
38
|
-
style?: Style;
|
|
39
|
-
}): {
|
|
35
|
+
export declare function toEndpoint(groupName: string, spec: IR, specOperation: TunedOperationObject, operation: Operation): {
|
|
40
36
|
schemas: string[];
|
|
41
37
|
};
|
|
42
38
|
export declare function toHttpOutput(spec: IR, operationName: string, status: string, response: ResponseObject, withGenerics?: boolean): string[];
|
package/dist/lib/sdk.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sdk.d.ts","sourceRoot":"","sources":["../../src/lib/sdk.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAIvE,OAAO,EACL,KAAK,EAAE,EAEP,KAAK,YAAY,EACjB,KAAK,oBAAoB,EAK1B,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"sdk.d.ts","sourceRoot":"","sources":["../../src/lib/sdk.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAIvE,OAAO,EACL,KAAK,EAAE,EAEP,KAAK,YAAY,EACjB,KAAK,oBAAoB,EAK1B,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAG1D,MAAM,MAAM,MAAM,GAAG,SAAS,GAAG,UAAU,CAAC;AAE5C,MAAM,WAAW,SAAS;IACxB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,IAAI;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,YAAY,EAAE,CAAC;IACxB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;IACxC,UAAU,EAAE,YAAY,CAAC;CAC1B;AAED,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;CAChB;AACD,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IACvC,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED,wBAAgB,UAAU,CACxB,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,EAAE,EACR,aAAa,EAAE,oBAAoB,EACnC,SAAS,EAAE,SAAS;;EA2CrB;AA0GD,wBAAgB,YAAY,CAC1B,IAAI,EAAE,EAAE,EACR,aAAa,EAAE,MAAM,EACrB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,cAAc,EACxB,YAAY,UAAO,YAqCpB;AAyCD,wBAAgB,WAAW,CACzB,SAAS,EAAE,oBAAoB,EAC/B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC;;;;;EA8BvC"}
|
package/dist/lib/style.d.ts
CHANGED
package/dist/lib/style.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"style.d.ts","sourceRoot":"","sources":["../../src/lib/style.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"style.d.ts","sourceRoot":"","sources":["../../src/lib/style.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,GAAG;IAClB,IAAI,CAAC,EAAE,QAAQ,CAAC;CACjB,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["export * from './lib/generate.js';\nexport * from './lib/generator.js';\nexport * from './lib/sdk.js';\nexport * from './lib/typescript-snippet.js';\n"],
|
|
5
|
+
"mappings": "AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { camelcase } from "stringcase";
|
|
2
|
+
import {
|
|
3
|
+
forEachOperation
|
|
4
|
+
} from "@sdk-it/spec";
|
|
5
|
+
function generateAISDKTools(ir) {
|
|
6
|
+
const groups = {};
|
|
7
|
+
forEachOperation(ir, (entry, operation) => {
|
|
8
|
+
const tagDef = ir.tags.find((tag) => tag.name === entry.tag);
|
|
9
|
+
if (!tagDef) {
|
|
10
|
+
console.warn(`No tag details found for tag: ${entry.tag}`);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
groups[entry.tag] ??= {
|
|
14
|
+
tools: [],
|
|
15
|
+
instructions: "",
|
|
16
|
+
displayName: "",
|
|
17
|
+
name: ""
|
|
18
|
+
};
|
|
19
|
+
groups[entry.tag].tools.push(createTool(entry, operation));
|
|
20
|
+
groups[entry.tag].instructions = tagDef["x-instructions"];
|
|
21
|
+
groups[entry.tag].name = tagDef.name;
|
|
22
|
+
groups[entry.tag].displayName = tagDef["x-name"];
|
|
23
|
+
});
|
|
24
|
+
const imports = [
|
|
25
|
+
`import { z } from 'zod';`,
|
|
26
|
+
`import { tool } from 'ai';`,
|
|
27
|
+
`import * as schemas from './inputs/index.ts';`
|
|
28
|
+
];
|
|
29
|
+
const agent = Object.entries(groups).map(
|
|
30
|
+
([group, { instructions, tools, displayName }]) => {
|
|
31
|
+
return `export const ${camelcase(group)} = {
|
|
32
|
+
name: '${displayName}',
|
|
33
|
+
instructions: \`${instructions}\`,
|
|
34
|
+
tools: { ${tools.join(", ")} }
|
|
35
|
+
}`;
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
return [...imports, ...agent].join("\n\n");
|
|
39
|
+
}
|
|
40
|
+
function createTool(entry, operation) {
|
|
41
|
+
const schemaName = camelcase(`${operation.operationId} schema`);
|
|
42
|
+
const toolInfo = operation["x-tool"];
|
|
43
|
+
return `'${toolInfo?.name || operation["x-fn-name"]}': tool({
|
|
44
|
+
description: \`${toolInfo?.description || operation.description || operation.summary}\`,
|
|
45
|
+
inputSchema: schemas.${schemaName},
|
|
46
|
+
execute: async (input, options) => {
|
|
47
|
+
console.log('Executing ${operation.operationId} tool with input:', input);
|
|
48
|
+
const context = coerceContext(options.experimental_context);
|
|
49
|
+
const response = await context.client.request(
|
|
50
|
+
'${entry.method.toUpperCase()} ${entry.path}' ,
|
|
51
|
+
input,
|
|
52
|
+
);
|
|
53
|
+
return JSON.stringify(response);
|
|
54
|
+
},
|
|
55
|
+
})`;
|
|
56
|
+
}
|
|
57
|
+
export {
|
|
58
|
+
generateAISDKTools
|
|
59
|
+
};
|
|
60
|
+
//# sourceMappingURL=ai-sdk.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/lib/agent/ai-sdk.ts"],
|
|
4
|
+
"sourcesContent": ["import { camelcase } from 'stringcase';\n\nimport {\n type IR,\n type OperationEntry,\n type TunedOperationObject,\n forEachOperation,\n} from '@sdk-it/spec';\n\nexport function generateAISDKTools(ir: IR) {\n const groups: Record<\n string,\n {\n tools: string[];\n instructions: string;\n displayName: string;\n name: string;\n }\n > = {};\n forEachOperation(ir, (entry, operation) => {\n const tagDef = ir.tags.find((tag) => tag.name === entry.tag);\n if (!tagDef) {\n console.warn(`No tag details found for tag: ${entry.tag}`);\n return;\n }\n\n groups[entry.tag] ??= {\n tools: [],\n instructions: '',\n displayName: '',\n name: '',\n };\n groups[entry.tag].tools.push(createTool(entry, operation));\n groups[entry.tag].instructions = tagDef['x-instructions'];\n groups[entry.tag].name = tagDef.name;\n groups[entry.tag].displayName = tagDef['x-name'];\n });\n const imports = [\n `import { z } from 'zod';`,\n `import { tool } from 'ai';`,\n `import * as schemas from './inputs/index.ts';`,\n ];\n const agent = Object.entries(groups).map(\n ([group, { instructions, tools, displayName }]) => {\n return `export const ${camelcase(group)} = {\n name: '${displayName}',\n instructions: \\`${instructions}\\`,\n tools: { ${tools.join(', ')} }\n }`;\n },\n );\n\n return [...imports, ...agent].join('\\n\\n');\n}\n\nfunction createTool(entry: OperationEntry, operation: TunedOperationObject) {\n const schemaName = camelcase(`${operation.operationId} schema`);\n const toolInfo = operation['x-tool'];\n return `'${toolInfo?.name || operation['x-fn-name']}': tool({\n description: \\`${toolInfo?.description || operation.description || operation.summary}\\`,\n inputSchema: schemas.${schemaName},\n\t\t\texecute: async (input, options) => {\n\t\t\t\tconsole.log('Executing ${operation.operationId} tool with input:', input);\n const context = coerceContext(options.experimental_context);\n const response = await context.client.request(\n '${entry.method.toUpperCase()} ${entry.path}' ,\n input,\n );\n return JSON.stringify(response);\n },\n })`;\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,iBAAiB;AAE1B;AAAA,EAIE;AAAA,OACK;AAEA,SAAS,mBAAmB,IAAQ;AACzC,QAAM,SAQF,CAAC;AACL,mBAAiB,IAAI,CAAC,OAAO,cAAc;AACzC,UAAM,SAAS,GAAG,KAAK,KAAK,CAAC,QAAQ,IAAI,SAAS,MAAM,GAAG;AAC3D,QAAI,CAAC,QAAQ;AACX,cAAQ,KAAK,iCAAiC,MAAM,GAAG,EAAE;AACzD;AAAA,IACF;AAEA,WAAO,MAAM,GAAG,MAAM;AAAA,MACpB,OAAO,CAAC;AAAA,MACR,cAAc;AAAA,MACd,aAAa;AAAA,MACb,MAAM;AAAA,IACR;AACA,WAAO,MAAM,GAAG,EAAE,MAAM,KAAK,WAAW,OAAO,SAAS,CAAC;AACzD,WAAO,MAAM,GAAG,EAAE,eAAe,OAAO,gBAAgB;AACxD,WAAO,MAAM,GAAG,EAAE,OAAO,OAAO;AAChC,WAAO,MAAM,GAAG,EAAE,cAAc,OAAO,QAAQ;AAAA,EACjD,CAAC;AACD,QAAM,UAAU;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACA,QAAM,QAAQ,OAAO,QAAQ,MAAM,EAAE;AAAA,IACnC,CAAC,CAAC,OAAO,EAAE,cAAc,OAAO,YAAY,CAAC,MAAM;AACjD,aAAO,gBAAgB,UAAU,KAAK,CAAC;AAAA,aAChC,WAAW;AAAA,sBACF,YAAY;AAAA,eACnB,MAAM,KAAK,IAAI,CAAC;AAAA;AAAA,IAE3B;AAAA,EACF;AAEA,SAAO,CAAC,GAAG,SAAS,GAAG,KAAK,EAAE,KAAK,MAAM;AAC3C;AAEA,SAAS,WAAW,OAAuB,WAAiC;AAC1E,QAAM,aAAa,UAAU,GAAG,UAAU,WAAW,SAAS;AAC9D,QAAM,WAAW,UAAU,QAAQ;AACnC,SAAO,IAAI,UAAU,QAAQ,UAAU,WAAW,CAAC;AAAA,uBAC9B,UAAU,eAAe,UAAU,eAAe,UAAU,OAAO;AAAA,6BAC7D,UAAU;AAAA;AAAA,6BAEV,UAAU,WAAW;AAAA;AAAA;AAAA,aAGrC,MAAM,OAAO,YAAY,CAAC,IAAI,MAAM,IAAI;AAAA;AAAA;AAAA;AAAA;AAAA;AAMrD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { camelcase, spinalcase } from "stringcase";
|
|
2
|
+
import {
|
|
3
|
+
forEachOperation
|
|
4
|
+
} from "@sdk-it/spec";
|
|
5
|
+
function generateOpenAIAgentTools(spec) {
|
|
6
|
+
const groups = {};
|
|
7
|
+
forEachOperation(spec, (entry, operation) => {
|
|
8
|
+
groups[entry.tag] ??= [];
|
|
9
|
+
groups[entry.tag].push(createTool(entry, operation));
|
|
10
|
+
});
|
|
11
|
+
const imports = [
|
|
12
|
+
`import { z } from 'zod';`,
|
|
13
|
+
`import { tool } from '@openai/agents';`,
|
|
14
|
+
`import * as schemas from './inputs/index.ts';`
|
|
15
|
+
];
|
|
16
|
+
const tools = Object.entries(groups).map(([group, tools2]) => {
|
|
17
|
+
return `export const ${spinalcase(group)} = [${tools2.join(", ")}];`;
|
|
18
|
+
});
|
|
19
|
+
return [...imports, ...tools].join("\n\n");
|
|
20
|
+
}
|
|
21
|
+
function createTool(entry, operation) {
|
|
22
|
+
const schemaName = camelcase(`${operation.operationId} schema`);
|
|
23
|
+
return `tool({
|
|
24
|
+
description: \`${operation.description || operation.summary}\`,
|
|
25
|
+
name: '${operation["x-fn-name"]}',
|
|
26
|
+
parameters: makeOptionalPropsNullable(schemas.${schemaName}),
|
|
27
|
+
execute: async (input, maybeContext) => {
|
|
28
|
+
console.log('Executing ${operation.operationId} tool with input:', input);
|
|
29
|
+
const context = coerceContext(maybeContext?.context);
|
|
30
|
+
const client = context.client;
|
|
31
|
+
const response = await client.request(
|
|
32
|
+
'${entry.method.toUpperCase()} ${entry.path}' ,
|
|
33
|
+
input as any,
|
|
34
|
+
);
|
|
35
|
+
return JSON.stringify(response);
|
|
36
|
+
},
|
|
37
|
+
})`;
|
|
38
|
+
}
|
|
39
|
+
export {
|
|
40
|
+
generateOpenAIAgentTools
|
|
41
|
+
};
|
|
42
|
+
//# sourceMappingURL=openai-agents.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/lib/agent/openai-agents.ts"],
|
|
4
|
+
"sourcesContent": ["import { camelcase, spinalcase } from 'stringcase';\n\nimport {\n type IR,\n type OperationEntry,\n type TunedOperationObject,\n forEachOperation,\n} from '@sdk-it/spec';\n\nexport function generateOpenAIAgentTools(spec: IR) {\n const groups: Record<string, string[]> = {};\n forEachOperation(spec, (entry, operation) => {\n groups[entry.tag] ??= [];\n groups[entry.tag].push(createTool(entry, operation));\n });\n const imports = [\n `import { z } from 'zod';`,\n `import { tool } from '@openai/agents';`,\n `import * as schemas from './inputs/index.ts';`,\n ];\n const tools = Object.entries(groups).map(([group, tools]) => {\n return `export const ${spinalcase(group)} = [${tools.join(', ')}];`;\n });\n\n return [...imports, ...tools].join('\\n\\n');\n}\n\nfunction createTool(entry: OperationEntry, operation: TunedOperationObject) {\n const schemaName = camelcase(`${operation.operationId} schema`);\n return `tool({\n description: \\`${operation.description || operation.summary}\\`,\n name: '${operation['x-fn-name']}',\n parameters: makeOptionalPropsNullable(schemas.${schemaName}),\n\t\t\texecute: async (input, maybeContext) => {\n\t\t\t\tconsole.log('Executing ${operation.operationId} tool with input:', input);\n const context = coerceContext(maybeContext?.context);\n const client = context.client;\n const response = await client.request(\n '${entry.method.toUpperCase()} ${entry.path}' ,\n input as any,\n );\n return JSON.stringify(response);\n },\n })`;\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,WAAW,kBAAkB;AAEtC;AAAA,EAIE;AAAA,OACK;AAEA,SAAS,yBAAyB,MAAU;AACjD,QAAM,SAAmC,CAAC;AAC1C,mBAAiB,MAAM,CAAC,OAAO,cAAc;AAC3C,WAAO,MAAM,GAAG,MAAM,CAAC;AACvB,WAAO,MAAM,GAAG,EAAE,KAAK,WAAW,OAAO,SAAS,CAAC;AAAA,EACrD,CAAC;AACD,QAAM,UAAU;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACA,QAAM,QAAQ,OAAO,QAAQ,MAAM,EAAE,IAAI,CAAC,CAAC,OAAOA,MAAK,MAAM;AAC3D,WAAO,gBAAgB,WAAW,KAAK,CAAC,OAAOA,OAAM,KAAK,IAAI,CAAC;AAAA,EACjE,CAAC;AAED,SAAO,CAAC,GAAG,SAAS,GAAG,KAAK,EAAE,KAAK,MAAM;AAC3C;AAEA,SAAS,WAAW,OAAuB,WAAiC;AAC1E,QAAM,aAAa,UAAU,GAAG,UAAU,WAAW,SAAS;AAC9D,SAAO;AAAA,uBACc,UAAU,eAAe,UAAU,OAAO;AAAA,eAClD,UAAU,WAAW,CAAC;AAAA,sDACiB,UAAU;AAAA;AAAA,6BAEnC,UAAU,WAAW;AAAA;AAAA;AAAA;AAAA,aAIrC,MAAM,OAAO,YAAY,CAAC,IAAI,MAAM,IAAI;AAAA;AAAA;AAAA;AAAA;AAAA;AAMrD;",
|
|
6
|
+
"names": ["tools"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { toLitObject } from "@sdk-it/core";
|
|
2
|
+
import { toZod } from "./emitters/zod.ts";
|
|
3
|
+
var client_default = (spec, style) => {
|
|
4
|
+
const defaultHeaders = `{${spec.options.filter((value) => value.in === "header").map(
|
|
5
|
+
(value) => `'${value.name}': options['${value["x-optionName"] ?? value.name}']`
|
|
6
|
+
).join(",\n")}}`;
|
|
7
|
+
const defaultInputs = `{${spec.options.filter((value) => value.in === "input").map(
|
|
8
|
+
(value) => `'${value.name}': options['${value["x-optionName"] ?? value.name}']`
|
|
9
|
+
).join(",\n")}}`;
|
|
10
|
+
const globalOptions = Object.fromEntries(
|
|
11
|
+
spec.options.map((value) => [
|
|
12
|
+
`'${value["x-optionName"] ?? value.name}'`,
|
|
13
|
+
{ schema: toZod(value.schema, value.required) }
|
|
14
|
+
])
|
|
15
|
+
);
|
|
16
|
+
const specOptions = {
|
|
17
|
+
...globalOptions,
|
|
18
|
+
...globalOptions["'token'"] ? {
|
|
19
|
+
"'token'": {
|
|
20
|
+
schema: `z.union([z.string(),z.function().returns(z.union([z.string(), z.promise(z.string())])),]).optional()
|
|
21
|
+
.transform(async (token) => {
|
|
22
|
+
if (!token) return undefined;
|
|
23
|
+
if (typeof token === 'function') {
|
|
24
|
+
token = await Promise.resolve(token());
|
|
25
|
+
}
|
|
26
|
+
return \`Bearer \${token}\`;
|
|
27
|
+
})`
|
|
28
|
+
}
|
|
29
|
+
} : {},
|
|
30
|
+
fetch: {
|
|
31
|
+
schema: "fetchType"
|
|
32
|
+
},
|
|
33
|
+
baseUrl: {
|
|
34
|
+
schema: spec.servers.length ? `z.enum(servers).default(servers[0])` : "z.string()"
|
|
35
|
+
},
|
|
36
|
+
headers: {
|
|
37
|
+
schema: "z.record(z.string()).optional()"
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
return `import z from 'zod';
|
|
41
|
+
import type { HeadersInit, RequestConfig } from './http/${spec.makeImport("request")}';
|
|
42
|
+
import { fetchType, parse } from './http/${spec.makeImport("dispatcher")}';
|
|
43
|
+
import schemas from './api/${spec.makeImport("schemas")}';
|
|
44
|
+
import {
|
|
45
|
+
createBaseUrlInterceptor,
|
|
46
|
+
createHeadersInterceptor,
|
|
47
|
+
} from './http/${spec.makeImport("interceptors")}';
|
|
48
|
+
|
|
49
|
+
import { type ParseError, parseInput } from './http/${spec.makeImport("parser")}';
|
|
50
|
+
|
|
51
|
+
${spec.servers.length ? `export const servers = ${JSON.stringify(spec.servers, null, 2)} as const` : ""}
|
|
52
|
+
const optionsSchema = z.object(${toLitObject(specOptions, (x) => x.schema)});
|
|
53
|
+
${spec.servers.length ? `export type Servers = typeof servers[number];` : ""}
|
|
54
|
+
|
|
55
|
+
type ${spec.name}Options = z.input<typeof optionsSchema>;
|
|
56
|
+
|
|
57
|
+
export class ${spec.name} {
|
|
58
|
+
public options: ${spec.name}Options;
|
|
59
|
+
constructor(options: ${spec.name}Options) {
|
|
60
|
+
this.options = options;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async request<const E extends keyof typeof schemas>(
|
|
64
|
+
endpoint: E,
|
|
65
|
+
input: z.input<(typeof schemas)[E]['schema']>,
|
|
66
|
+
options?: { signal?: AbortSignal, headers?: HeadersInit },
|
|
67
|
+
) ${style.errorAsValue ? `: Promise<Awaited<ReturnType<(typeof schemas)[E]['dispatch']>>| [never, ParseError<(typeof schemas)[E]['schema']>]>` : `: Promise<Awaited<ReturnType<(typeof schemas)[E]['dispatch']>>>`} {
|
|
68
|
+
const route = schemas[endpoint];
|
|
69
|
+
const withDefaultInputs = Object.assign({}, await this.#defaultInputs(), input);
|
|
70
|
+
const [parsedInput, parseError] = parseInput(route.schema, withDefaultInputs);
|
|
71
|
+
if (parseError) {
|
|
72
|
+
${style.errorAsValue ? "return [null as never, parseError as never] as const;" : "throw parseError;"}
|
|
73
|
+
}
|
|
74
|
+
const clientOptions = await optionsSchema.parseAsync(this.options);
|
|
75
|
+
const result = await route.dispatch(parsedInput as never, {
|
|
76
|
+
fetch: clientOptions.fetch,
|
|
77
|
+
interceptors: [
|
|
78
|
+
createHeadersInterceptor(
|
|
79
|
+
await this.#defaultHeaders(),
|
|
80
|
+
options?.headers ?? {},
|
|
81
|
+
),
|
|
82
|
+
createBaseUrlInterceptor(clientOptions.baseUrl),
|
|
83
|
+
],
|
|
84
|
+
signal: options?.signal,
|
|
85
|
+
});
|
|
86
|
+
return result as Awaited<ReturnType<(typeof schemas)[E]['dispatch']>>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async prepare<const E extends keyof typeof schemas>(
|
|
90
|
+
endpoint: E,
|
|
91
|
+
input: z.input<(typeof schemas)[E]['schema']>,
|
|
92
|
+
options?: { headers?: HeadersInit },
|
|
93
|
+
): ${style.errorAsValue ? `Promise<
|
|
94
|
+
readonly [
|
|
95
|
+
RequestConfig & {
|
|
96
|
+
parse: (response: Response) => ReturnType<typeof parse>;
|
|
97
|
+
},
|
|
98
|
+
ParseError<(typeof schemas)[E]['schema']> | null,
|
|
99
|
+
]
|
|
100
|
+
>` : `Promise<RequestConfig & {
|
|
101
|
+
parse: (response: Response) => ReturnType<typeof parse>;
|
|
102
|
+
}>`} {
|
|
103
|
+
const clientOptions = await optionsSchema.parseAsync(this.options);
|
|
104
|
+
const route = schemas[endpoint];
|
|
105
|
+
const interceptors = [
|
|
106
|
+
createHeadersInterceptor(
|
|
107
|
+
await this.#defaultHeaders(),
|
|
108
|
+
options?.headers ?? {},
|
|
109
|
+
),
|
|
110
|
+
createBaseUrlInterceptor(clientOptions.baseUrl),
|
|
111
|
+
];
|
|
112
|
+
const [parsedInput, parseError] = parseInput(route.schema, input);
|
|
113
|
+
if (parseError) {
|
|
114
|
+
${style.errorAsValue ? "return [null as never, parseError as never] as const;" : "throw parseError;"}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let config = route.toRequest(parsedInput as never);
|
|
118
|
+
for (const interceptor of interceptors) {
|
|
119
|
+
if (interceptor.before) {
|
|
120
|
+
config = await interceptor.before(config);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const prepared = { ...config, parse: (response: Response) => parse(route.output, response) as never };
|
|
124
|
+
return ${style.errorAsValue ? "[prepared, null as never] as const;" : "prepared as any"}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async #defaultHeaders() {
|
|
128
|
+
const options = await optionsSchema.parseAsync(this.options);
|
|
129
|
+
return {
|
|
130
|
+
...${defaultHeaders},
|
|
131
|
+
...options.headers,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async #defaultInputs() {
|
|
136
|
+
const options = await optionsSchema.parseAsync(this.options);
|
|
137
|
+
return ${defaultInputs}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
setOptions(options: Partial<${spec.name}Options>) {
|
|
141
|
+
this.options = {
|
|
142
|
+
...this.options,
|
|
143
|
+
...options,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
}`;
|
|
148
|
+
};
|
|
149
|
+
export {
|
|
150
|
+
client_default as default
|
|
151
|
+
};
|
|
152
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/lib/client.ts"],
|
|
4
|
+
"sourcesContent": ["import { toLitObject } from '@sdk-it/core';\n\nimport { toZod } from './emitters/zod.ts';\nimport type { Spec } from './sdk.ts';\nimport type { Style } from './style.ts';\n\nexport default (spec: Omit<Spec, 'operations'>, style: Style) => {\n const defaultHeaders = `{${spec.options\n .filter((value) => value.in === 'header')\n .map(\n (value) =>\n `'${value.name}': options['${value['x-optionName'] ?? value.name}']`,\n )\n .join(',\\n')}}`;\n const defaultInputs = `{${spec.options\n .filter((value) => value.in === 'input')\n .map(\n (value) =>\n `'${value.name}': options['${value['x-optionName'] ?? value.name}']`,\n )\n .join(',\\n')}}`;\n\n /**\n * Map of option name to zod schema (as string)\n * Usually stuff like token, apiKey and so on.\n */\n const globalOptions = Object.fromEntries(\n spec.options.map((value) => [\n `'${value['x-optionName'] ?? value.name}'`,\n { schema: toZod(value.schema, value.required) },\n ]),\n );\n\n const specOptions: Record<string, { schema: string }> = {\n ...globalOptions,\n ...(globalOptions[\"'token'\"]\n ? {\n \"'token'\": {\n schema: `z.union([z.string(),z.function().returns(z.union([z.string(), z.promise(z.string())])),]).optional()\n .transform(async (token) => {\n if (!token) return undefined;\n if (typeof token === 'function') {\n token = await Promise.resolve(token());\n }\n return \\`Bearer \\${token}\\`;\n })`,\n },\n }\n : {}),\n fetch: {\n schema: 'fetchType',\n },\n baseUrl: {\n schema: spec.servers.length\n ? `z.enum(servers).default(servers[0])`\n : 'z.string()',\n },\n headers: {\n schema: 'z.record(z.string()).optional()',\n },\n };\n\n return `import z from 'zod';\nimport type { HeadersInit, RequestConfig } from './http/${spec.makeImport('request')}';\nimport { fetchType, parse } from './http/${spec.makeImport('dispatcher')}';\nimport schemas from './api/${spec.makeImport('schemas')}';\nimport {\n createBaseUrlInterceptor,\n createHeadersInterceptor,\n} from './http/${spec.makeImport('interceptors')}';\n\nimport { type ParseError, parseInput } from './http/${spec.makeImport('parser')}';\n\n${spec.servers.length ? `export const servers = ${JSON.stringify(spec.servers, null, 2)} as const` : ''}\nconst optionsSchema = z.object(${toLitObject(specOptions, (x) => x.schema)});\n${spec.servers.length ? `export type Servers = typeof servers[number];` : ''}\n\ntype ${spec.name}Options = z.input<typeof optionsSchema>;\n\nexport class ${spec.name} {\n public options: ${spec.name}Options;\n constructor(options: ${spec.name}Options) {\n this.options = options;\n }\n\n async request<const E extends keyof typeof schemas>(\n endpoint: E,\n input: z.input<(typeof schemas)[E]['schema']>,\n options?: { signal?: AbortSignal, headers?: HeadersInit },\n ) ${style.errorAsValue ? `: Promise<Awaited<ReturnType<(typeof schemas)[E]['dispatch']>>| [never, ParseError<(typeof schemas)[E]['schema']>]>` : `: Promise<Awaited<ReturnType<(typeof schemas)[E]['dispatch']>>>`} {\n const route = schemas[endpoint];\n const withDefaultInputs = Object.assign({}, await this.#defaultInputs(), input);\n const [parsedInput, parseError] = parseInput(route.schema, withDefaultInputs);\n if (parseError) {\n ${style.errorAsValue ? 'return [null as never, parseError as never] as const;' : 'throw parseError;'}\n }\n const clientOptions = await optionsSchema.parseAsync(this.options);\n const result = await route.dispatch(parsedInput as never, {\n fetch: clientOptions.fetch,\n interceptors: [\n createHeadersInterceptor(\n await this.#defaultHeaders(),\n options?.headers ?? {},\n ),\n createBaseUrlInterceptor(clientOptions.baseUrl),\n ],\n signal: options?.signal,\n });\n return result as Awaited<ReturnType<(typeof schemas)[E]['dispatch']>>;\n }\n\n async prepare<const E extends keyof typeof schemas>(\n endpoint: E,\n input: z.input<(typeof schemas)[E]['schema']>,\n options?: { headers?: HeadersInit },\n ): ${\n style.errorAsValue\n ? `Promise<\n readonly [\n RequestConfig & {\n parse: (response: Response) => ReturnType<typeof parse>;\n },\n ParseError<(typeof schemas)[E]['schema']> | null,\n ]\n >`\n : `Promise<RequestConfig & {\n parse: (response: Response) => ReturnType<typeof parse>;\n }>`\n } {\n const clientOptions = await optionsSchema.parseAsync(this.options);\n const route = schemas[endpoint];\n const interceptors = [\n createHeadersInterceptor(\n await this.#defaultHeaders(),\n options?.headers ?? {},\n ),\n createBaseUrlInterceptor(clientOptions.baseUrl),\n ];\n const [parsedInput, parseError] = parseInput(route.schema, input);\n if (parseError) {\n ${style.errorAsValue ? 'return [null as never, parseError as never] as const;' : 'throw parseError;'}\n }\n\n let config = route.toRequest(parsedInput as never);\n for (const interceptor of interceptors) {\n if (interceptor.before) {\n config = await interceptor.before(config);\n }\n }\n const prepared = { ...config, parse: (response: Response) => parse(route.output, response) as never };\n return ${style.errorAsValue ? '[prepared, null as never] as const;' : 'prepared as any'}\n }\n\n async #defaultHeaders() {\n const options = await optionsSchema.parseAsync(this.options);\n return {\n ...${defaultHeaders},\n ...options.headers,\n };\n }\n\n async #defaultInputs() {\n const options = await optionsSchema.parseAsync(this.options);\n return ${defaultInputs}\n }\n\n setOptions(options: Partial<${spec.name}Options>) {\n this.options = {\n ...this.options,\n ...options,\n };\n }\n\n}`;\n};\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,mBAAmB;AAE5B,SAAS,aAAa;AAItB,IAAO,iBAAQ,CAAC,MAAgC,UAAiB;AAC/D,QAAM,iBAAiB,IAAI,KAAK,QAC7B,OAAO,CAAC,UAAU,MAAM,OAAO,QAAQ,EACvC;AAAA,IACC,CAAC,UACC,IAAI,MAAM,IAAI,eAAe,MAAM,cAAc,KAAK,MAAM,IAAI;AAAA,EACpE,EACC,KAAK,KAAK,CAAC;AACd,QAAM,gBAAgB,IAAI,KAAK,QAC5B,OAAO,CAAC,UAAU,MAAM,OAAO,OAAO,EACtC;AAAA,IACC,CAAC,UACC,IAAI,MAAM,IAAI,eAAe,MAAM,cAAc,KAAK,MAAM,IAAI;AAAA,EACpE,EACC,KAAK,KAAK,CAAC;AAMd,QAAM,gBAAgB,OAAO;AAAA,IAC3B,KAAK,QAAQ,IAAI,CAAC,UAAU;AAAA,MAC1B,IAAI,MAAM,cAAc,KAAK,MAAM,IAAI;AAAA,MACvC,EAAE,QAAQ,MAAM,MAAM,QAAQ,MAAM,QAAQ,EAAE;AAAA,IAChD,CAAC;AAAA,EACH;AAEA,QAAM,cAAkD;AAAA,IACtD,GAAG;AAAA,IACH,GAAI,cAAc,SAAS,IACvB;AAAA,MACE,WAAW;AAAA,QACT,QAAQ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAQV;AAAA,IACF,IACA,CAAC;AAAA,IACL,OAAO;AAAA,MACL,QAAQ;AAAA,IACV;AAAA,IACA,SAAS;AAAA,MACP,QAAQ,KAAK,QAAQ,SACjB,wCACA;AAAA,IACN;AAAA,IACA,SAAS;AAAA,MACP,QAAQ;AAAA,IACV;AAAA,EACF;AAEA,SAAO;AAAA,0DACiD,KAAK,WAAW,SAAS,CAAC;AAAA,2CACzC,KAAK,WAAW,YAAY,CAAC;AAAA,6BAC3C,KAAK,WAAW,SAAS,CAAC;AAAA;AAAA;AAAA;AAAA,iBAItC,KAAK,WAAW,cAAc,CAAC;AAAA;AAAA,sDAEM,KAAK,WAAW,QAAQ,CAAC;AAAA;AAAA,EAE7E,KAAK,QAAQ,SAAS,0BAA0B,KAAK,UAAU,KAAK,SAAS,MAAM,CAAC,CAAC,cAAc,EAAE;AAAA,iCACtE,YAAY,aAAa,CAAC,MAAM,EAAE,MAAM,CAAC;AAAA,EACxE,KAAK,QAAQ,SAAS,kDAAkD,EAAE;AAAA;AAAA,OAErE,KAAK,IAAI;AAAA;AAAA,eAED,KAAK,IAAI;AAAA,oBACJ,KAAK,IAAI;AAAA,yBACJ,KAAK,IAAI;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAQ5B,MAAM,eAAe,wHAAwH,iEAAiE;AAAA;AAAA;AAAA;AAAA;AAAA,QAK5M,MAAM,eAAe,0DAA0D,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,OAsBtG,MAAM,eACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,OAQA;AAAA;AAAA,SAGN;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,QAYM,MAAM,eAAe,0DAA0D,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,aAU7F,MAAM,eAAe,wCAAwC,iBAAiB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,WAMhF,cAAc;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,aAOZ,aAAa;AAAA;AAAA;AAAA,gCAGM,KAAK,IAAI;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAQzC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|