Skip to content

Commit

Permalink
feat(breaking change): update apiClient arguments interface (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon committed Apr 1, 2023
1 parent afcb023 commit a9e0a14
Show file tree
Hide file tree
Showing 24 changed files with 14,987 additions and 2,178 deletions.
8 changes: 4 additions & 4 deletions jest.snapshot.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ module.exports = {
"@swc/jest",
{
jsc: {
parser :{
syntax: "typescript"
}
}
parser: {
syntax: "typescript",
},
},
},
],
},
Expand Down
72 changes: 45 additions & 27 deletions src/code-templates/_shared/ApiClientInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,33 +117,10 @@ export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Pa
],
});

const httpMethod = factory.ParameterDeclaration.create({
name: "httpMethod",
const requestArgs = factory.ParameterDeclaration.create({
name: "requestArgs",
type: factory.TypeReferenceNode.create({
name: "HttpMethod",
}),
});
const url = factory.ParameterDeclaration.create({
name: "url",
type: factory.TypeNode.create({ type: "string" }),
});
const headers = factory.ParameterDeclaration.create({
name: "headers",
type: objectLikeOrAnyType,
});
const requestBody = factory.ParameterDeclaration.create({
name: "requestBody",
type: objectLikeOrAnyType,
});
const queryParameters = factory.ParameterDeclaration.create({
name: "queryParameters",
type: factory.UnionTypeNode.create({
typeNodes: [
factory.TypeReferenceNode.create({
name: "QueryParameters",
}),
factory.TypeNode.create({ type: "undefined" }),
],
name: "RequestArgs",
}),
});
const options = factory.ParameterDeclaration.create({
Expand Down Expand Up @@ -186,7 +163,7 @@ export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Pa
}),
}),
],
parameters: [httpMethod, url, headers, requestBody, queryParameters, options],
parameters: [requestArgs, options],
type: returnType,
});

Expand All @@ -196,12 +173,53 @@ export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Pa
type: functionType,
});

const requestArgsInterfaceDeclaration = factory.InterfaceDeclaration.create({
export: true,
name: "RequestArgs",
members: [
factory.PropertySignature.create({
name: `httpMethod`,
optional: false,
type: factory.TypeReferenceNode.create({ name: "HttpMethod" }),
}),
factory.PropertySignature.create({
name: `url`,
optional: false,
type: factory.TypeReferenceNode.create({ name: "string" }),
}),
factory.PropertySignature.create({
name: `headers`,
optional: false,
type: objectLikeOrAnyType,
}),
factory.PropertySignature.create({
name: `requestBody`,
optional: false,
type: objectLikeOrAnyType,
}),
factory.PropertySignature.create({
name: `queryParameters`,
optional: false,
type: factory.UnionTypeNode.create({
typeNodes: [
factory.TypeReferenceNode.create({
name: "QueryParameters",
}),
factory.TypeNode.create({ type: "undefined" }),
],
}),
}),
],
typeParameters: [],
});

return [
createHttpMethod(factory),
createObjectLikeInterface(factory),
...createQueryParamsDeclarations(factory),
createSuccessResponseTypeAlias("SuccessResponses", factory, successResponseNames),
errorResponseNamespace,
requestArgsInterfaceDeclaration,
factory.InterfaceDeclaration.create({
export: true,
name: "ApiClient",
Expand Down
44 changes: 32 additions & 12 deletions src/code-templates/_shared/MethodBody/CallRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,38 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.
function: "apiClient.request",
};
const expression = Utils.generateVariableIdentifier(factory, apiClientVariableIdentifier[methodType]);
const argumentsArray = [
factory.StringLiteral.create({ text: params.operationParams.httpMethod.toUpperCase() }),
factory.Identifier.create({ name: "url" }),
factory.Identifier.create({ name: "headers" }),
convertedParams.hasRequestBody
? Utils.generateVariableIdentifier(factory, "params.requestBody")
: factory.Identifier.create({ name: "undefined" }),
convertedParams.hasQueryParameters
? factory.Identifier.create({ name: "queryParameters" })
: factory.Identifier.create({ name: "undefined" }),
factory.Identifier.create({ name: "option" }),
];

const requestArgs = factory.ObjectLiteralExpression.create({
properties: [
factory.PropertyAssignment.create({
name: "httpMethod",
initializer: factory.StringLiteral.create({ text: params.operationParams.httpMethod.toUpperCase() }),
}),
factory.PropertyAssignment.create({
name: "url",
initializer: factory.Identifier.create({ name: "url" }),
}),
factory.PropertyAssignment.create({
name: "headers",
initializer: factory.Identifier.create({ name: "headers" }),
}),
factory.PropertyAssignment.create({
name: "requestBody",
initializer: convertedParams.hasRequestBody
? Utils.generateVariableIdentifier(factory, "params.requestBody")
: factory.Identifier.create({ name: "undefined" }),
}),
factory.PropertyAssignment.create({
name: "queryParameters",
initializer: convertedParams.hasQueryParameters
? factory.Identifier.create({ name: "queryParameters" })
: factory.Identifier.create({ name: "undefined" }),
}),
],
multiLine: true,
});

const argumentsArray = [requestArgs, factory.Identifier.create({ name: "option" })];

return factory.CallExpression.create({
expression: expression,
Expand Down
4 changes: 2 additions & 2 deletions src/code-templates/_shared/MethodBody/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.
const queryParameter = pickedParameters.filter(item => item.in === "query");
const queryObject = Object.values(queryParameter).reduce<{ [key: string]: QueryParameter.Item }>((previous, current) => {
const { text, escaped } = escapeText(current.name);
const variableDeclaraText = escaped ? `params.parameter[${text}]` : `params.parameter.${text}`;
const variableDeclareText = escaped ? `params.parameter[${text}]` : `params.parameter.${text}`;
return {
...previous,
[current.name]: { type: "variable", value: variableDeclaraText, style: current.style, explode: !!current.explode },
[current.name]: { type: "variable", value: variableDeclareText, style: current.style, explode: !!current.explode },
};
}, {});
statements.push(QueryParameter.create(factory, { variableName: "queryParameters", object: queryObject }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export const create = (factory: TsGenerator.Factory.Type): ts.TypeAliasDeclarati
name: "ClientFunction<RequestOption>",
type: factory.TypeReferenceNode.create({
name: `typeof createClient<RequestOption>`,
})
}),
}),
factory.TypeAliasDeclaration.create({
export: true,
name: "Client<RequestOption>",
type: factory.TypeReferenceNode.create({
name: `ReturnType<ClientFunction<RequestOption>>`,
})
})
]
}),
}),
];
};
4 changes: 2 additions & 2 deletions src/code-templates/functional-api-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const generator: CodeGenerator.GenerateFunction<Option> = (
statements.push(apiClientStatement);
ClientTypeDefinition.create(factory).forEach(statement => {
statements.push(statement);
})
});

return statements;
};
1 change: 0 additions & 1 deletion src/internal/TsGenerator/factory/VariableDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ts from "typescript";
import { generateComment } from "./utils";

export interface Params {
name: string | ts.BindingName;
Expand Down
2 changes: 0 additions & 2 deletions src/internal/TsGenerator/factory/VariableDeclarationList.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ts from "typescript";
import { generateComment } from "./utils";

const flags = {
const: ts.NodeFlags.Const,
Expand All @@ -8,7 +7,6 @@ const flags = {
export interface Params {
declarations: readonly ts.VariableDeclaration[];
flag: keyof typeof flags;

}

export interface Factory {
Expand Down
Loading

0 comments on commit a9e0a14

Please sign in to comment.