-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1699 from hey-api/fix/sdk-meta-field
fix: allow passing arbitrary values to SDK functions via meta field
- Loading branch information
Showing
100 changed files
with
612 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@hey-api/openapi-ts': patch | ||
--- | ||
|
||
fix: allow passing arbitrary values to SDK functions via `meta` field |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
packages/openapi-ts/src/plugins/@hey-api/sdk/typeOptions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { compiler } from '../../../compiler'; | ||
import { clientModulePath } from '../../../generate/client'; | ||
import type { FileImportResult } from '../../../generate/files'; | ||
import type { IR } from '../../../ir/types'; | ||
import type { Plugin } from '../../types'; | ||
import { getClientPlugin } from '../client-core/utils'; | ||
import { sdkId } from './plugin'; | ||
import type { Config } from './types'; | ||
|
||
export const createTypeOptions = ({ | ||
clientOptions, | ||
context, | ||
plugin, | ||
}: { | ||
clientOptions: FileImportResult; | ||
context: IR.Context; | ||
plugin: Plugin.Instance<Config>; | ||
}) => { | ||
const file = context.file({ id: sdkId })!; | ||
const client = getClientPlugin(context.config); | ||
const isNuxtClient = client.name === '@hey-api/client-nuxt'; | ||
|
||
const clientModule = clientModulePath({ | ||
config: context.config, | ||
sourceOutput: file.nameWithoutExtension(), | ||
}); | ||
const tDataShape = file.import({ | ||
asType: true, | ||
module: clientModule, | ||
name: 'TDataShape', | ||
}); | ||
const clientType = file.import({ | ||
asType: true, | ||
module: clientModule, | ||
name: 'Client', | ||
}); | ||
|
||
const typeOptions = compiler.typeAliasDeclaration({ | ||
exportType: true, | ||
name: 'Options', | ||
type: compiler.typeIntersectionNode({ | ||
types: [ | ||
compiler.typeReferenceNode({ | ||
typeArguments: isNuxtClient | ||
? [ | ||
compiler.typeReferenceNode({ typeName: 'TComposable' }), | ||
compiler.typeReferenceNode({ typeName: 'TData' }), | ||
] | ||
: [ | ||
compiler.typeReferenceNode({ typeName: 'TData' }), | ||
compiler.typeReferenceNode({ typeName: 'ThrowOnError' }), | ||
], | ||
typeName: clientOptions.name, | ||
}), | ||
compiler.typeInterfaceNode({ | ||
properties: [ | ||
{ | ||
comment: [ | ||
'You can provide a client instance returned by `createClient()` instead of', | ||
'individual options. This might be also useful if you want to implement a', | ||
'custom client.', | ||
], | ||
isRequired: !plugin.client, | ||
name: 'client', | ||
type: compiler.typeReferenceNode({ typeName: clientType.name }), | ||
}, | ||
{ | ||
comment: [ | ||
'You can pass arbitrary values through the `meta` object. This can be', | ||
"used to access values that aren't defined as part of the SDK function.", | ||
], | ||
isRequired: false, | ||
name: 'meta', | ||
type: compiler.typeReferenceNode({ | ||
typeArguments: [ | ||
compiler.keywordTypeNode({ keyword: 'string' }), | ||
compiler.keywordTypeNode({ keyword: 'unknown' }), | ||
], | ||
typeName: 'Record', | ||
}), | ||
}, | ||
], | ||
useLegacyResolution: false, | ||
}), | ||
], | ||
}), | ||
typeParameters: isNuxtClient | ||
? [ | ||
compiler.typeParameterDeclaration({ | ||
constraint: compiler.typeReferenceNode({ typeName: 'Composable' }), | ||
name: 'TComposable', | ||
}), | ||
compiler.typeParameterDeclaration({ | ||
constraint: compiler.typeReferenceNode({ | ||
typeName: tDataShape.name, | ||
}), | ||
defaultType: compiler.typeReferenceNode({ | ||
typeName: tDataShape.name, | ||
}), | ||
name: 'TData', | ||
}), | ||
] | ||
: [ | ||
compiler.typeParameterDeclaration({ | ||
constraint: compiler.typeReferenceNode({ | ||
typeName: tDataShape.name, | ||
}), | ||
defaultType: compiler.typeReferenceNode({ | ||
typeName: tDataShape.name, | ||
}), | ||
name: 'TData', | ||
}), | ||
compiler.typeParameterDeclaration({ | ||
constraint: compiler.keywordTypeNode({ keyword: 'boolean' }), | ||
defaultType: compiler.keywordTypeNode({ keyword: 'boolean' }), | ||
name: 'ThrowOnError', | ||
}), | ||
], | ||
}); | ||
|
||
file.add(typeOptions); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.