-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: generate typed baseUrl option
- Loading branch information
Showing
347 changed files
with
3,062 additions
and
424 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
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
119 changes: 119 additions & 0 deletions
119
packages/openapi-ts/src/openApi/2.0.x/parser/__tests__/server.test.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,119 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import type { IR } from '../../../../ir/types'; | ||
import type { OpenApi } from '../../../types'; | ||
import { parseServers } from '../server'; | ||
|
||
describe('parseServers', () => { | ||
it('host + basePath + schemes', () => { | ||
const context: Partial<IR.Context<Partial<OpenApi.V2_0_X>>> = { | ||
// @ts-expect-error | ||
config: { | ||
input: { | ||
path: '', | ||
}, | ||
}, | ||
ir: {}, | ||
spec: { | ||
basePath: '/v1', | ||
host: 'foo.com', | ||
schemes: ['http', 'https'], | ||
}, | ||
}; | ||
parseServers({ context: context as IR.Context }); | ||
expect(context.ir!.servers).toEqual([ | ||
{ | ||
url: 'http://foo.com/v1', | ||
}, | ||
{ | ||
url: 'https://foo.com/v1', | ||
}, | ||
]); | ||
}); | ||
|
||
it('schemes + host', () => { | ||
const context: Partial<IR.Context<Partial<OpenApi.V2_0_X>>> = { | ||
// @ts-expect-error | ||
config: { | ||
input: { | ||
path: '', | ||
}, | ||
}, | ||
ir: {}, | ||
spec: { | ||
host: 'foo.com', | ||
schemes: ['ws'], | ||
}, | ||
}; | ||
parseServers({ context: context as IR.Context }); | ||
expect(context.ir!.servers).toEqual([ | ||
{ | ||
url: 'ws://foo.com', | ||
}, | ||
]); | ||
}); | ||
|
||
it('host + basePath', () => { | ||
const context: Partial<IR.Context<Partial<OpenApi.V2_0_X>>> = { | ||
// @ts-expect-error | ||
config: { | ||
input: { | ||
path: '', | ||
}, | ||
}, | ||
ir: {}, | ||
spec: { | ||
basePath: '/v1', | ||
host: 'foo.com', | ||
}, | ||
}; | ||
parseServers({ context: context as IR.Context }); | ||
expect(context.ir!.servers).toEqual([ | ||
{ | ||
url: 'foo.com/v1', | ||
}, | ||
]); | ||
}); | ||
|
||
it('host', () => { | ||
const context: Partial<IR.Context<Partial<OpenApi.V2_0_X>>> = { | ||
// @ts-expect-error | ||
config: { | ||
input: { | ||
path: '', | ||
}, | ||
}, | ||
ir: {}, | ||
spec: { | ||
host: 'foo.com', | ||
}, | ||
}; | ||
parseServers({ context: context as IR.Context }); | ||
expect(context.ir!.servers).toEqual([ | ||
{ | ||
url: 'foo.com', | ||
}, | ||
]); | ||
}); | ||
|
||
it('basePath', () => { | ||
const context: Partial<IR.Context<Partial<OpenApi.V2_0_X>>> = { | ||
// @ts-expect-error | ||
config: { | ||
input: { | ||
path: '', | ||
}, | ||
}, | ||
ir: {}, | ||
spec: { | ||
basePath: '/v1', | ||
}, | ||
}; | ||
parseServers({ context: context as IR.Context }); | ||
expect(context.ir!.servers).toEqual([ | ||
{ | ||
url: '/v1', | ||
}, | ||
]); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { IR } from '../../../ir/types'; | ||
import { parseUrl } from '../../../utils/url'; | ||
|
||
export const parseServers = ({ context }: { context: IR.Context }) => { | ||
let schemes: ReadonlyArray<string> = context.spec.schemes ?? []; | ||
let host = context.spec.host ?? ''; | ||
const path = context.spec.basePath ?? ''; | ||
|
||
if (typeof context.config.input.path === 'string') { | ||
const url = parseUrl(context.config.input.path); | ||
|
||
if (!schemes.length) { | ||
if (url.protocol) { | ||
schemes = [url.protocol] as typeof schemes; | ||
} | ||
} | ||
|
||
if (!host) { | ||
host = `${url.host}${url.port ? `:${url.port}` : ''}`; | ||
} | ||
} | ||
|
||
if (!schemes.length) { | ||
schemes = ['']; | ||
} | ||
|
||
const servers = schemes | ||
.map((scheme) => `${scheme ? `${scheme}://` : ''}${host}${path}`) | ||
.filter(Boolean); | ||
|
||
if (servers.length) { | ||
context.ir.servers = servers.map((url) => ({ | ||
url, | ||
})); | ||
} | ||
}; |
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
78 changes: 78 additions & 0 deletions
78
packages/openapi-ts/src/plugins/@hey-api/typescript/clientOptions.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,78 @@ | ||
import ts from 'typescript'; | ||
|
||
import { compiler } from '../../../compiler'; | ||
import type { Identifier } from '../../../generate/files'; | ||
import type { IR } from '../../../ir/types'; | ||
import { parseUrl } from '../../../utils/url'; | ||
import type { Plugin } from '../../types'; | ||
import { getClientBaseUrlKey } from '../client-core/utils'; | ||
import { typesId } from './ref'; | ||
import type { Config } from './types'; | ||
|
||
const stringType = compiler.keywordTypeNode({ keyword: 'string' }); | ||
|
||
const serverToBaseUrlType = ({ server }: { server: IR.ServerObject }) => { | ||
const url = parseUrl(server.url); | ||
|
||
if (url.protocol && url.host) { | ||
return compiler.literalTypeNode({ | ||
literal: compiler.stringLiteral({ text: server.url }), | ||
}); | ||
} | ||
|
||
return compiler.templateLiteralType({ | ||
value: [ | ||
url.protocol || stringType, | ||
'://', | ||
url.host || stringType, | ||
url.port ? `:${url.port}` : '', | ||
url.path || '', | ||
], | ||
}); | ||
}; | ||
|
||
export const createClientOptions = ({ | ||
context, | ||
identifier, | ||
servers, | ||
}: { | ||
context: IR.Context; | ||
identifier: Identifier; | ||
plugin: Plugin.Instance<Config>; | ||
servers: ReadonlyArray<IR.ServerObject>; | ||
}) => { | ||
const file = context.file({ id: typesId })!; | ||
|
||
if (!identifier.name) { | ||
return; | ||
} | ||
|
||
const typeClientOptions = compiler.typeAliasDeclaration({ | ||
exportType: true, | ||
name: identifier.name, | ||
type: compiler.typeInterfaceNode({ | ||
properties: [ | ||
{ | ||
name: getClientBaseUrlKey(context.config), | ||
type: compiler.typeUnionNode({ | ||
types: [ | ||
...servers.map((server) => | ||
serverToBaseUrlType({ | ||
server, | ||
}), | ||
), | ||
servers.length | ||
? compiler.typeIntersectionNode({ | ||
types: [stringType, ts.factory.createTypeLiteralNode([])], | ||
}) | ||
: stringType, | ||
], | ||
}), | ||
}, | ||
], | ||
useLegacyResolution: false, | ||
}), | ||
}); | ||
|
||
file.add(typeClientOptions); | ||
}; |
Oops, something went wrong.