Skip to content
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
1 change: 1 addition & 0 deletions packages/openapi-ts/src/plugins/@hey-api/sdk/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const defaultConfig: Plugin.Config<Config> = {
}
},
asClass: false,
asInstance: false,
auth: true,
client: true,
exportFromIndex: true,
Expand Down
51 changes: 43 additions & 8 deletions packages/openapi-ts/src/plugins/@hey-api/sdk/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,31 @@ const operationStatements = ({
name: 'client',
});

const thisClient = plugin.asInstance
? compiler.propertyAccessExpression({
expression: compiler.identifier({ text: 'this' }),
isOptional: false,
name: 'client',
})
: undefined;

let clientExpression;
if (heyApiClient?.name) {
clientExpression = compiler.binaryExpression({
left: optionsClient,
operator: '??',
right: compiler.identifier({ text: heyApiClient.name }),
});
} else if (thisClient) {
clientExpression = compiler.binaryExpression({
left: optionsClient,
operator: '??',
right: thisClient,
});
} else {
clientExpression = optionsClient;
}

return [
compiler.returnFunctionCall({
args: [
Expand All @@ -497,13 +522,7 @@ const operationStatements = ({
}),
],
name: compiler.propertyAccessExpression({
expression: heyApiClient?.name
? compiler.binaryExpression({
left: optionsClient,
operator: '??',
right: compiler.identifier({ text: heyApiClient.name }),
})
: optionsClient,
expression: clientExpression,
name: compiler.identifier({ text: operation.method }),
}),
types: isNuxtClient
Expand Down Expand Up @@ -545,7 +564,7 @@ const generateClassSdk = ({
operation.summary && escapeComment(operation.summary),
operation.description && escapeComment(operation.description),
],
isStatic: true,
isStatic: !plugin.asInstance,
name: serviceFunctionIdentifier({
config: context.config,
handleIllegal: false,
Expand Down Expand Up @@ -618,6 +637,22 @@ const generateClassSdk = ({

context.subscribe('after', () => {
for (const [name, nodes] of sdks) {
if (plugin.asInstance) {
nodes.unshift(
compiler.constructorDeclaration({
multiLine: false,
parameters: [
{
accessLevel: 'public',
isReadOnly: false,
name: 'client',
type: 'Client',
},
],
}),
);
}

const node = compiler.classDeclaration({
decorator: undefined,
members: nodes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const createTypeOptions = ({
'individual options. This might be also useful if you want to implement a',
'custom client.',
],
isRequired: !plugin.client,
isRequired: !plugin.client && !plugin.asInstance,
name: 'client',
type: compiler.typeReferenceNode({ typeName: clientType.name }),
},
Expand Down
7 changes: 7 additions & 0 deletions packages/openapi-ts/src/plugins/@hey-api/sdk/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export interface Config extends Plugin.Name<'@hey-api/sdk'> {
* @default false
*/
asClass?: boolean;
/**
* SDK functions are non-static methods and the class must be instantiated
* with a client.
*
* @default false
*/
asInstance?: boolean;
/**
* Should the generated functions contain auth mechanisms? You may want to
* disable this option if you're handling auth yourself or defining it
Expand Down