Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace axios and other dependencies with native counterparts, add type support for stream #17

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.DS_Store
*.log
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "openai-openapi",
"repository": "https://github.com/openai/openai-openapi.git",
"dependencies": {},
"devDependencies": {
"@types/node": "^18.14.0",
"ts-morph": "^17.0.1",
"tsx": "^3.12.3"
}
}
5 changes: 5 additions & 0 deletions scripts/generate_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ def generate_sdk(sanitized_spec_path, sdk_type, output_path):
if sdk_type == "node":
template_override_path = os.path.join(os.path.dirname(
__file__), "../sdk-template-overrides/typescript-axios")
stream_ast = os.path.join(os.path.dirname(__file__), "./override_stream.ts")
data_source = os.path.join(output_path, "./**/*{.d.ts,.ts}")

os.system(
f"openapi-generator generate -i {sanitized_spec_path} -g typescript-axios -o {output_path} -p supportsES6=true -t {template_override_path}")

os.system(f"npx tsx '{stream_ast}' '{data_source}'")
else:
print(f"Unsupported SDK type {sdk_type}, skipping SDK generation")

Expand Down
181 changes: 181 additions & 0 deletions scripts/override_stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import {
Project,
Node,
ts,
CodeBlockWriter,
OptionalKind,
ParameterDeclarationStructure,
MethodDeclaration,
} from "ts-morph";

function writeBody(writer: CodeBlockWriter, argName: string, body: string) {
return writer
.write(`if (${argName}.stream) `)
.write(
body
.replace("createRequestFunction", "createStreamFunction")
.replace(argName, `{ ...${argName}, stream: true }`)
)
.write(" else ")
.write(body.replace(argName, `{ ...${argName}, stream: false }`));
}

function transformParameters(
kind: "stream" | "json",
parameters: OptionalKind<ParameterDeclarationStructure>[] | undefined
) {
return parameters?.map((param, idx) => {
return idx === 0
? {
...param,
type:
kind === "json"
? `${param.type} & { stream?: false }`
: `${param.type} & { stream: true }`,
}
: param;
});
}

function extractKeyTypes(method: MethodDeclaration) {
const requestName = method.getParameters()[0].getName();
const requestType = method.getParameters()[0].getTypeNodeOrThrow().getText();
const responseType = requestType.replace("Request", "Response");

return { requestName, requestType, responseType };
}

function transformObject(methodName: string, sourceNode: Node) {
const method = sourceNode
.getDescendantsOfKind(ts.SyntaxKind.MethodDeclaration)
.filter((declaration) => declaration.getName() === methodName)[0]!;

const keyTypes = extractKeyTypes(method);
const body = method
.getBodyOrThrow()
.getText({ trimLeadingIndentation: true });

const docs = method.getJsDocs()[0].getText({ trimLeadingIndentation: true });
const structure = method.getStructure();
const returnType = structure.returnType as string;

method
.getParent()
.asKindOrThrow(ts.SyntaxKind.ObjectLiteralExpression)
.insertPropertyAssignment(method.getChildIndex(), {
name: methodName,
leadingTrivia: docs + "\n",
initializer: (writer) =>
writer
.write("(() => {")
.indent(() => {
writer
.setIndentationLevel(writer.getIndentationLevel() - 1)
.write(`function ${methodName}(): unknown`)
.inlineBlock(() => writeBody(writer, keyTypes.requestName, body))
.write(";");

writer.writeLine(`return ${methodName};`);
})
.write("})()"),
})
.getFirstDescendantByKindOrThrow(ts.SyntaxKind.FunctionDeclaration)
.set({
parameters: structure.parameters,
isAsync: method.isAsync(),
overloads: [
{
isAsync: method.isAsync(),
parameters: transformParameters("json", structure.parameters),
returnType,
},
{
isAsync: method.isAsync(),
parameters: transformParameters("stream", structure.parameters),
returnType: returnType.replace(
keyTypes.responseType,
"ReadableStream"
),
},
],
returnType: returnType.replace(
keyTypes.responseType,
`${keyTypes.responseType} | ReadableStream`
),
});

method.remove();
}

function transformClass(methodName: string, sourceNode: Node) {
const method = sourceNode
.getDescendantsOfKind(ts.SyntaxKind.MethodDeclaration)
.filter((declaration) => declaration.getName() === methodName)[0]!;

const keyTypes = extractKeyTypes(method);

const body = method
.getBodyOrThrow()
.getText({ trimLeadingIndentation: true });
const docs = method.getJsDocs()[0].getText({ trimLeadingIndentation: true });
const structure = method.getStructure();

method.set({
name: methodName,
scope: structure.scope,
parameters: structure.parameters,
docs: [],
overloads: [
{
leadingTrivia: docs + "\n",
parameters: transformParameters("stream", structure.parameters),
returnType: `AxiosPromise<ReadableStream>`,
},
{
parameters: transformParameters("json", structure.parameters),
returnType: `AxiosPromise<${keyTypes.responseType}>`,
},
],
statements: (writer) =>
writer.indent(() => {
writer.withIndentationLevel(writer.getIndentationLevel() - 1, () => {
writeBody(writer, keyTypes.requestName, body);
});
}),
});
}

const project = new Project();
project.addSourceFilesAtPaths(process.argv[process.argv.length - 1]);

const sourceFile = project.getSourceFileOrThrow("api.ts");
const declarations = sourceFile.getExportedDeclarations();

const methods = new Set(
sourceFile
.getInterfaces()
.filter((declaration) => {
return declaration
.getDescendantsOfKind(ts.SyntaxKind.PropertySignature)
.some((a) => a.getName().includes(`'stream'`));
})
.map((decl) =>
decl
.findReferencesAsNodes()
.map((refs) =>
refs
.getFirstAncestorByKind(ts.SyntaxKind.MethodDeclaration)
?.getName()
)
)
.flat(2)
.filter((x): x is string => !!x)
);

for (const method of methods) {
transformObject(method, declarations.get("OpenAIApiFp")![0]);
transformObject(method, declarations.get("OpenAIApiFactory")![0]);
transformClass(method, declarations.get("OpenAIApi")![0]);
}

sourceFile.saveSync();
31 changes: 31 additions & 0 deletions sdk-template-overrides/typescript-axios/api.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* tslint:disable */
/* eslint-disable */
{{>licenseInfo}}

{{^withSeparateModelsAndApi}}
import type { Configuration } from './configuration';
{{#withNodeImports}}
// URLSearchParams not necessarily used
// @ts-ignore
import { URL, URLSearchParams } from 'url';
{{#multipartFormData}}
import FormData from 'form-data'
{{/multipartFormData}}
{{/withNodeImports}}
// Some imports not used depending on template conditions
// @ts-ignore
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction, createStreamFunction } from './common';
import type { RequestArgs, AxiosRequestConfig, AxiosPromise } from './base';
// @ts-ignore
import { BASE_PATH, COLLECTION_FORMATS, BaseAPI, RequiredError } from './base';

{{#models}}
{{#model}}{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^isEnum}}{{^oneOf}}{{>modelGeneric}}{{/oneOf}}{{/isEnum}}{{/model}}
{{/models}}
{{#apiInfo}}{{#apis}}
{{>apiInner}}
{{/apis}}{{/apiInfo}}
{{/withSeparateModelsAndApi}}{{#withSeparateModelsAndApi}}
{{#apiInfo}}{{#apis}}{{#operations}}export * from './{{tsApiPackage}}/{{classFilename}}';
{{/operations}}{{/apis}}{{/apiInfo}}
{{/withSeparateModelsAndApi}}
15 changes: 8 additions & 7 deletions sdk-template-overrides/typescript-axios/apiInner.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
/* eslint-disable */
{{>licenseInfo}}

import globalAxios, { AxiosPromise, AxiosInstance, AxiosRequestConfig } from 'axios';
import { Configuration } from '{{apiRelativeToRoot}}configuration';
// Some imports not used depending on template conditions
// @ts-ignore
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction } from '{{apiRelativeToRoot}}common';
// @ts-ignore
import { AxiosPromise, AxiosRequestConfig } from '{{apiRelativeToRoot}}base';
// @ts-ignore
import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } from '{{apiRelativeToRoot}}base';
{{#imports}}
// @ts-ignore
Expand Down Expand Up @@ -223,9 +224,9 @@ export const {{classname}}Fp = function(configuration?: Configuration) {
* @deprecated{{/isDeprecated}}
* @throws {RequiredError}
*/
async {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
async {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: AxiosRequestConfig): Promise<(basePath?: string) => AxiosPromise<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
return createRequestFunction(localVarAxiosArgs, BASE_PATH, configuration);
},
{{/operation}}
}
Expand All @@ -236,7 +237,7 @@ export const {{classname}}Fp = function(configuration?: Configuration) {
* {{&description}}{{/description}}
* @export
*/
export const {{classname}}Factory = function (configuration?: Configuration, basePath?: string, axios?: AxiosInstance) {
export const {{classname}}Factory = function (configuration?: Configuration, basePath?: string) {
const localVarFp = {{classname}}Fp(configuration)
return {
{{#operation}}
Expand All @@ -253,7 +254,7 @@ export const {{classname}}Factory = function (configuration?: Configuration, bas
* @throws {RequiredError}
*/
{{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: any): AxiosPromise<{{{returnType}}}{{^returnType}}void{{/returnType}}> {
return localVarFp.{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options).then((request) => request(axios, basePath));
return localVarFp.{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options).then((request) => request(basePath));
},
{{/operation}}
};
Expand Down Expand Up @@ -348,12 +349,12 @@ export class {{classname}} extends BaseAPI {
*/
{{#useSingleRequestParameter}}
public {{nickname}}({{#allParams.0}}requestParameters: {{classname}}{{operationIdCamelCase}}Request{{^hasRequiredParams}} = {}{{/hasRequiredParams}}, {{/allParams.0}}options?: AxiosRequestConfig) {
return {{classname}}Fp(this.configuration).{{nickname}}({{#allParams.0}}{{#allParams}}requestParameters.{{paramName}}, {{/allParams}}{{/allParams.0}}options).then((request) => request(this.axios, this.basePath));
return {{classname}}Fp(this.configuration).{{nickname}}({{#allParams.0}}{{#allParams}}requestParameters.{{paramName}}, {{/allParams}}{{/allParams.0}}options).then((request) => request(this.basePath));
}
{{/useSingleRequestParameter}}
{{^useSingleRequestParameter}}
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: AxiosRequestConfig) {
return {{classname}}Fp(this.configuration).{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options).then((request) => request(this.axios, this.basePath));
return {{classname}}Fp(this.configuration).{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}options).then((request) => request(this.basePath));
}
{{/useSingleRequestParameter}}
{{^-last}}
Expand Down
Loading