@sdk-it/typescript 0.29.0 → 0.30.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/index.js +38 -31
- package/dist/index.js.map +2 -2
- package/dist/lib/agent/ai-sdk.d.ts +2 -2
- package/dist/lib/agent/ai-sdk.d.ts.map +1 -1
- package/dist/lib/agent/openai-agents.d.ts +2 -2
- package/dist/lib/agent/openai-agents.d.ts.map +1 -1
- package/dist/lib/emitters/snippet.d.ts +2 -2
- package/dist/lib/emitters/snippet.d.ts.map +1 -1
- package/dist/lib/emitters/zod.d.ts +0 -1
- package/dist/lib/emitters/zod.d.ts.map +1 -1
- package/dist/lib/generator.d.ts +9 -3
- package/dist/lib/generator.d.ts.map +1 -1
- package/dist/lib/readme/readme.d.ts +2 -2
- package/dist/lib/readme/readme.d.ts.map +1 -1
- package/dist/lib/typescript-snippet.d.ts +2 -2
- package/dist/lib/typescript-snippet.d.ts.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -11,11 +11,11 @@ import {
|
|
|
11
11
|
writeFiles
|
|
12
12
|
} from "@sdk-it/core/file-system.js";
|
|
13
13
|
import {
|
|
14
|
-
augmentSpec,
|
|
15
14
|
cleanFiles,
|
|
16
15
|
readWriteMetadata,
|
|
17
16
|
sanitizeTag as sanitizeTag4,
|
|
18
|
-
securityToOptions as securityToOptions2
|
|
17
|
+
securityToOptions as securityToOptions2,
|
|
18
|
+
toIR
|
|
19
19
|
} from "@sdk-it/spec";
|
|
20
20
|
|
|
21
21
|
// packages/typescript/src/lib/agent/ai-sdk.ts
|
|
@@ -47,8 +47,7 @@ function createTool(entry, operation) {
|
|
|
47
47
|
inputSchema: schemas.${schemaName},
|
|
48
48
|
execute: async (input) => {
|
|
49
49
|
console.log('Executing ${operation.operationId} tool with input:', input);
|
|
50
|
-
const
|
|
51
|
-
const response = await client.request(
|
|
50
|
+
const response = await context.client.request(
|
|
52
51
|
'${entry.method.toUpperCase()} ${entry.path}' ,
|
|
53
52
|
input as any,
|
|
54
53
|
);
|
|
@@ -959,28 +958,10 @@ function generateCode(config) {
|
|
|
959
958
|
"text/plain": "text"
|
|
960
959
|
};
|
|
961
960
|
for (const type in operation.requestBody.content) {
|
|
962
|
-
|
|
963
|
-
config.spec,
|
|
964
|
-
|
|
965
|
-
type
|
|
961
|
+
schemas[shortContenTypeMap[type]] = zodDeserialzer.handle(
|
|
962
|
+
operationSchema(config.spec, operation, type),
|
|
963
|
+
true
|
|
966
964
|
);
|
|
967
|
-
const additionalProperties = {};
|
|
968
|
-
for (const [name, prop] of Object.entries(xProperties)) {
|
|
969
|
-
additionalProperties[name] = {
|
|
970
|
-
name,
|
|
971
|
-
required: xRequired?.includes(name),
|
|
972
|
-
schema: prop,
|
|
973
|
-
in: prop["x-in"]
|
|
974
|
-
};
|
|
975
|
-
}
|
|
976
|
-
const schema = merge({}, objectSchema, {
|
|
977
|
-
required: Object.values(additionalProperties).filter((p) => p.required).map((p) => p.name),
|
|
978
|
-
properties: Object.entries(additionalProperties).reduce(
|
|
979
|
-
(acc, [, p]) => ({ ...acc, [p.name]: p.schema }),
|
|
980
|
-
{}
|
|
981
|
-
)
|
|
982
|
-
});
|
|
983
|
-
schemas[shortContenTypeMap[type]] = zodDeserialzer.handle(schema, true);
|
|
984
965
|
}
|
|
985
966
|
const details = buildInput(config.spec, operation);
|
|
986
967
|
const endpoint = toEndpoint(
|
|
@@ -1107,7 +1088,7 @@ function bodyInputs(spec, ctSchema) {
|
|
|
1107
1088
|
{}
|
|
1108
1089
|
);
|
|
1109
1090
|
}
|
|
1110
|
-
var
|
|
1091
|
+
var contentTypeSerializerMap = {
|
|
1111
1092
|
"application/json": "json",
|
|
1112
1093
|
"application/x-www-form-urlencoded": "urlencoded",
|
|
1113
1094
|
"multipart/form-data": "formdata",
|
|
@@ -1115,10 +1096,13 @@ var contentTypeMap = {
|
|
|
1115
1096
|
"text/plain": "text",
|
|
1116
1097
|
"application/empty": "empty"
|
|
1117
1098
|
};
|
|
1099
|
+
var serializerContentTypeMap = Object.fromEntries(
|
|
1100
|
+
Object.entries(contentTypeSerializerMap).map(([k, v]) => [v, k])
|
|
1101
|
+
);
|
|
1118
1102
|
function buildInput(spec, operation) {
|
|
1119
1103
|
const inputs = {};
|
|
1120
1104
|
let outgoingContentType = "empty";
|
|
1121
|
-
for (const [ct, value] of Object.entries(
|
|
1105
|
+
for (const [ct, value] of Object.entries(contentTypeSerializerMap)) {
|
|
1122
1106
|
if (operation.requestBody.content[ct]) {
|
|
1123
1107
|
outgoingContentType = value;
|
|
1124
1108
|
const { objectSchema, xProperties } = coearceRequestInput(
|
|
@@ -1138,9 +1122,33 @@ function buildInput(spec, operation) {
|
|
|
1138
1122
|
}
|
|
1139
1123
|
return {
|
|
1140
1124
|
inputs,
|
|
1141
|
-
outgoingContentType
|
|
1125
|
+
outgoingContentType,
|
|
1126
|
+
ct: serializerContentTypeMap[outgoingContentType]
|
|
1142
1127
|
};
|
|
1143
1128
|
}
|
|
1129
|
+
function operationSchema(ir, operation, type) {
|
|
1130
|
+
const { objectSchema, xProperties, xRequired } = coearceRequestInput(
|
|
1131
|
+
ir,
|
|
1132
|
+
operation,
|
|
1133
|
+
type
|
|
1134
|
+
);
|
|
1135
|
+
const additionalProperties = {};
|
|
1136
|
+
for (const [name, prop] of Object.entries(xProperties)) {
|
|
1137
|
+
additionalProperties[name] = {
|
|
1138
|
+
name,
|
|
1139
|
+
required: xRequired?.includes(name),
|
|
1140
|
+
schema: prop,
|
|
1141
|
+
in: prop["x-in"]
|
|
1142
|
+
};
|
|
1143
|
+
}
|
|
1144
|
+
return merge({}, objectSchema, {
|
|
1145
|
+
required: Object.values(additionalProperties).filter((p) => p.required).map((p) => p.name),
|
|
1146
|
+
properties: Object.entries(additionalProperties).reduce(
|
|
1147
|
+
(acc, [, p]) => ({ ...acc, [p.name]: p.schema }),
|
|
1148
|
+
{}
|
|
1149
|
+
)
|
|
1150
|
+
});
|
|
1151
|
+
}
|
|
1144
1152
|
|
|
1145
1153
|
// packages/typescript/src/lib/http/dispatcher.txt
|
|
1146
1154
|
var dispatcher_default = "export type Unionize<T> = T extends [infer Single extends OutputType]\n ? InstanceType<Single>\n : T extends readonly [...infer Tuple extends OutputType[]]\n ? { [I in keyof Tuple]: InstanceType<Tuple[I]> }[number]\n : never;\n\nexport type InstanceType<T> =\n T extends Type<infer U>\n ? U\n : T extends { type: Type<infer U> }\n ? U\n : T extends Array<unknown>\n ? Unionize<T>\n : never;\n\nexport interface Type<T> {\n new (...args: any[]): T;\n}\nexport type Parser = (\n response: Response,\n) => Promise<unknown> | ReadableStream<any>;\nexport type OutputType =\n | Type<APIResponse>\n | { parser: Parser; type: Type<APIResponse> };\n\nexport const fetchType = z\n .function()\n .args(z.instanceof(Request))\n .returns(z.promise(z.instanceof(Response)))\n .optional();\n\nexport async function parse<T extends OutputType[]>(\n outputs: T,\n response: Response,\n) <% if(!throwError) { %>\n: Promise<\n [\n Extract<InstanceType<T>, SuccessfulResponse>['data'],\n Extract<InstanceType<T>, ProblematicResponse>['data'],\n ]\n>\n <% } %>\n\n\n\n {\n let output: typeof APIResponse | null = null;\n let parser: Parser = buffered;\n for (const outputType of outputs) {\n if ('parser' in outputType) {\n parser = outputType.parser;\n if (isTypeOf(outputType.type, APIResponse)) {\n if (response.status === outputType.type.status) {\n output = outputType.type;\n break;\n }\n }\n } else if (isTypeOf(outputType, APIResponse)) {\n if (response.status === outputType.status) {\n output = outputType;\n break;\n }\n }\n }\n\n\n if (response.ok) {\n const apiresponse = (output || APIResponse).create(\n response.status,\n await parser(response),\n );\n <% if(throwError) { %>\n return <% if (outputType === 'default') { %>apiresponse as Extract<InstanceType<T>, SuccessfulResponse><% } else { %>apiresponse as Extract<InstanceType<T>, SuccessfulResponse>;<% } %>;\n <% } else { %>\n return [<% if (outputType === 'default') { %>apiresponse.data as Extract<InstanceType<T>, SuccessfulResponse>['data']<% } else { %>apiresponse as Extract<InstanceType<T>, SuccessfulResponse><% } %>, null] as const;\n <% } %>\n }\n<% if(throwError) { %>\n throw (output || APIError).create(\n response.status,\n await parser(response),\n );\n<% } else { %>\n const data = (output || APIError).create(\n response.status,\n await parser(response),\n );\n return [null, data] as const;\n<% } %>\n}\n\nexport function isTypeOf<T extends Type<APIResponse>>(\n instance: any,\n baseType: T,\n): instance is T {\n if (instance === baseType) {\n return true;\n }\n const prototype = Object.getPrototypeOf(instance);\n if (prototype === null) {\n return false;\n }\n return isTypeOf(prototype, baseType);\n}\n\nexport class Dispatcher {\n #interceptors: Interceptor[] = [];\n #fetch: z.infer<typeof fetchType>;\n constructor(interceptors: Interceptor[], fetch?: z.infer<typeof fetchType>) {\n this.#interceptors = interceptors;\n this.#fetch = fetch;\n }\n\n async send<T extends OutputType[]>(\n config: RequestConfig,\n outputs: T,\n signal?: AbortSignal,\n ) {\n for (const interceptor of this.#interceptors) {\n if (interceptor.before) {\n config = await interceptor.before(config);\n }\n }\n\n let response = await (this.#fetch ?? fetch)(\n new Request(config.url, config.init),\n {\n ...config.init,\n signal: signal,\n },\n );\n\n for (let i = this.#interceptors.length - 1; i >= 0; i--) {\n const interceptor = this.#interceptors[i];\n if (interceptor.after) {\n response = await interceptor.after(response.clone());\n }\n }\n\n return await parse(outputs, response);\n }\n}\n";
|
|
@@ -2188,13 +2196,11 @@ var SnippetEmitter = class {
|
|
|
2188
2196
|
// packages/typescript/src/lib/typescript-snippet.ts
|
|
2189
2197
|
var TypeScriptSnippet = class {
|
|
2190
2198
|
#spec;
|
|
2191
|
-
#settings;
|
|
2192
2199
|
#snippetEmitter;
|
|
2193
2200
|
#clientName;
|
|
2194
2201
|
#packageName;
|
|
2195
2202
|
constructor(spec, settings) {
|
|
2196
2203
|
this.#spec = spec;
|
|
2197
|
-
this.#settings = settings;
|
|
2198
2204
|
this.#snippetEmitter = new SnippetEmitter(spec);
|
|
2199
2205
|
this.#clientName = settings.name?.trim() ? pascalcase4(settings.name) : "Client";
|
|
2200
2206
|
this.#packageName = settings.name ? `@${spinalcase3(this.#clientName.toLowerCase())}/sdk` : "sdk";
|
|
@@ -2941,7 +2947,7 @@ function security(spec) {
|
|
|
2941
2947
|
return options;
|
|
2942
2948
|
}
|
|
2943
2949
|
async function generate(openapi, settings) {
|
|
2944
|
-
const spec =
|
|
2950
|
+
const spec = toIR(
|
|
2945
2951
|
{
|
|
2946
2952
|
spec: openapi,
|
|
2947
2953
|
responses: { flattenErrorResponses: true },
|
|
@@ -3254,6 +3260,7 @@ export {
|
|
|
3254
3260
|
generate,
|
|
3255
3261
|
generateCode,
|
|
3256
3262
|
inputToPath,
|
|
3263
|
+
operationSchema,
|
|
3257
3264
|
toEndpoint,
|
|
3258
3265
|
toHttpOutput,
|
|
3259
3266
|
toInputs
|