-
-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add `@orval/fetch` package * chore: add `fetch` tests * docs: add `fetch` in valid values of `client` option on guide * chore: update `fetch` package to `6.29.1` * fix: query params join fetch url * fix: considering the case of intentional null specification * fix: removed `qs` import to reduce dependent libraries * fix: remove unnecessary object creation processes * chore: update `yarn.lock` and deps
- Loading branch information
1 parent
ce95b68
commit d1cf87f
Showing
11 changed files
with
285 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[![npm version](https://badge.fury.io/js/orval.svg)](https://badge.fury.io/js/orval) | ||
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) | ||
[![tests](https://github.com/anymaniax/orval/actions/workflows/tests.yaml/badge.svg)](https://github.com/anymaniax/orval/actions/workflows/tests.yaml) | ||
|
||
<p align="center"> | ||
<img src="./logo/orval-logo-horizontal.svg?raw=true" width="500" height="160" alt="orval - Restfull Client Generator" /> | ||
</p> | ||
<h1 align="center"> | ||
Visit <a href="https://orval.dev" target="_blank">orval.dev</a> for docs, guides, API and beer! | ||
</h1> | ||
|
||
### Code Generation | ||
|
||
`orval` is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in `yaml` or `json` formats. | ||
|
||
`Generate`, `valid`, `cache` and `mock` in your React, Vue, Svelte and Angular applications all with your OpenAPI specification. | ||
|
||
### Samples | ||
|
||
You can find below some samples | ||
|
||
- [react app](https://github.com/anymaniax/orval/tree/master/samples/react-app) | ||
- [react query](https://github.com/anymaniax/orval/tree/master/samples/react-query) | ||
- [svelte query](https://github.com/anymaniax/orval/tree/master/samples/svelte-query) | ||
- [vue query](https://github.com/anymaniax/orval/tree/master/samples/vue-query) | ||
- [react app with swr](https://github.com/anymaniax/orval/tree/master/samples/react-app-with-swr) | ||
- [nx fastify react](https://github.com/anymaniax/orval/tree/master/samples/nx-fastify-react) | ||
- [angular app](https://github.com/anymaniax/orval/tree/master/samples/angular-app) | ||
- [hono](https://github.com/anymaniax/orval/tree/master/samples/hono) |
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,18 @@ | ||
{ | ||
"name": "@orval/fetch", | ||
"version": "6.29.1", | ||
"license": "MIT", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "tsup ./src/index.ts --target node12 --clean --sourcemap --dts", | ||
"dev": "tsup ./src/index.ts --target node12 --clean --sourcemap --watch src", | ||
"lint": "eslint src/**/*.ts" | ||
}, | ||
"dependencies": { | ||
"@orval/core": "6.29.1" | ||
} | ||
} |
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,144 @@ | ||
import { | ||
camel, | ||
ClientBuilder, | ||
ClientDependenciesBuilder, | ||
ClientGeneratorsBuilder, | ||
generateFormDataAndUrlEncodedFunction, | ||
generateVerbImports, | ||
GeneratorDependency, | ||
GeneratorOptions, | ||
GeneratorVerbOptions, | ||
GetterPropType, | ||
stringify, | ||
toObjectString, | ||
generateBodyOptions, | ||
isObject, | ||
} from '@orval/core'; | ||
|
||
const generateRequestFunction = ( | ||
{ | ||
queryParams, | ||
operationName, | ||
response, | ||
body, | ||
props, | ||
verb, | ||
formData, | ||
formUrlEncoded, | ||
override, | ||
}: GeneratorVerbOptions, | ||
{ route }: GeneratorOptions, | ||
) => { | ||
const isRequestOptions = override?.requestOptions !== false; | ||
const isFormData = override?.formData !== false; | ||
const isFormUrlEncoded = override?.formUrlEncoded !== false; | ||
|
||
const getUrlFnName = camel(`get-${operationName}-url`); | ||
const getUrlFnProps = toObjectString( | ||
props.filter( | ||
(prop) => | ||
prop.type === GetterPropType.PARAM || | ||
prop.type === GetterPropType.NAMED_PATH_PARAMS || | ||
prop.type === GetterPropType.QUERY_PARAM, | ||
), | ||
'implementation', | ||
); | ||
const getUrlFnImplementation = `export const ${getUrlFnName} = (${getUrlFnProps}) => { | ||
${ | ||
queryParams | ||
? ` | ||
const normalizedParams = new URLSearchParams(); | ||
Object.entries(params || {}).forEach(([key, value]) => { | ||
if (value === null) { | ||
normalizedParams.append(key, 'null'); | ||
} else if (value !== undefined) { | ||
normalizedParams.append(key, value.toString()); | ||
} | ||
});` | ||
: '' | ||
} | ||
return \`${route}${queryParams ? '?${normalizedParams.toString()}' : ''}\` | ||
}\n`; | ||
const getUrlFnProperties = props | ||
.filter( | ||
(prop) => | ||
prop.type === GetterPropType.PARAM || | ||
prop.type === GetterPropType.QUERY_PARAM || | ||
prop.type === GetterPropType.NAMED_PATH_PARAMS, | ||
) | ||
.map((param) => { | ||
if (param.type === GetterPropType.NAMED_PATH_PARAMS) { | ||
return param.destructured; | ||
} else { | ||
return param.name; | ||
} | ||
}) | ||
.join(','); | ||
|
||
const args = `${toObjectString(props, 'implementation')} ${isRequestOptions ? `options?: RequestInit` : ''}`; | ||
const retrunType = `Promise<${response.definition.success || 'unknown'}>`; | ||
|
||
const globalFetchOptions = isObject(override?.requestOptions) | ||
? `${stringify(override?.requestOptions)?.slice(1, -1)?.trim()}` | ||
: ''; | ||
const fetchMethodOption = `method: '${verb.toUpperCase()}'`; | ||
|
||
const requestBodyParams = generateBodyOptions( | ||
body, | ||
isFormData, | ||
isFormUrlEncoded, | ||
); | ||
const fetchBodyOption = requestBodyParams | ||
? `body: JSON.stringify(${requestBodyParams})` | ||
: ''; | ||
|
||
const fetchResponseImplementation = `const res = await fetch( | ||
${getUrlFnName}(${getUrlFnProperties}), | ||
{${globalFetchOptions ? '\n' : ''} ${globalFetchOptions} | ||
${isRequestOptions ? '...options,' : ''} | ||
${fetchMethodOption}${fetchBodyOption ? ',' : ''} | ||
${fetchBodyOption} | ||
} | ||
) | ||
return res.json() | ||
`; | ||
|
||
const bodyForm = generateFormDataAndUrlEncodedFunction({ | ||
formData, | ||
formUrlEncoded, | ||
body, | ||
isFormData, | ||
isFormUrlEncoded, | ||
}); | ||
|
||
const fetchImplementationBody = | ||
`${bodyForm ? ` ${bodyForm}\n` : ''}` + ` ${fetchResponseImplementation}`; | ||
const fetchImplementation = `export const ${operationName} = async (${args}): ${retrunType} => {\n${fetchImplementationBody}}`; | ||
|
||
const implementation = | ||
`${getUrlFnImplementation}\n` + `${fetchImplementation}\n`; | ||
|
||
return implementation; | ||
}; | ||
|
||
export const generateClient: ClientBuilder = (verbOptions, options) => { | ||
const imports = generateVerbImports(verbOptions); | ||
const functionImplementation = generateRequestFunction(verbOptions, options); | ||
|
||
return { | ||
implementation: `${functionImplementation}\n`, | ||
imports, | ||
}; | ||
}; | ||
|
||
const fetchClientBuilder: ClientGeneratorsBuilder = { | ||
client: generateClient, | ||
dependencies: () => [], | ||
}; | ||
|
||
export const builder = () => () => fetchClientBuilder; | ||
|
||
export default builder; |
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,4 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"include": ["src/**/*.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
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,75 @@ | ||
import { defineConfig } from 'orval'; | ||
|
||
export default defineConfig({ | ||
petstore: { | ||
output: { | ||
target: '../generated/fetch/petstore/endpoints.ts', | ||
schemas: '../generated/fetch/petstore/model', | ||
mock: true, | ||
client: 'axios', | ||
}, | ||
input: { | ||
target: '../specifications/petstore.yaml', | ||
}, | ||
}, | ||
multiArguments: { | ||
output: { | ||
target: '../generated/fetch/multi-arguments/endpoints.ts', | ||
schemas: '../generated/fetch/multi-arguments/model', | ||
mock: true, | ||
client: 'fetch', | ||
}, | ||
input: { | ||
target: '../specifications/petstore.yaml', | ||
}, | ||
}, | ||
petstoreTagsSplit: { | ||
output: { | ||
target: '../generated/fetch/petstore-tags-split/endpoints.ts', | ||
schemas: '../generated/fetch/petstore-tags-split/model', | ||
mock: true, | ||
mode: 'tags-split', | ||
client: 'fetch', | ||
}, | ||
input: { | ||
target: '../specifications/petstore.yaml', | ||
}, | ||
}, | ||
petstoreSplit: { | ||
output: { | ||
target: '../generated/fetch/split/endpoints.ts', | ||
schemas: '../generated/fetch/split/model', | ||
mock: true, | ||
mode: 'split', | ||
client: 'fetch', | ||
}, | ||
input: { | ||
target: '../specifications/petstore.yaml', | ||
}, | ||
}, | ||
petstoreTags: { | ||
output: { | ||
target: '../generated/fetch/tags/endpoints.ts', | ||
schemas: '../generated/fetch/tags/model', | ||
mock: true, | ||
mode: 'tags', | ||
client: 'fetch', | ||
}, | ||
input: { | ||
target: '../specifications/petstore.yaml', | ||
}, | ||
}, | ||
namedParameters: { | ||
output: { | ||
target: '../generated/fetch/named-parameters/endpoints.ts', | ||
schemas: '../generated/fetch/named-parameters/model', | ||
client: 'fetch', | ||
override: { | ||
useNamedParameters: true, | ||
}, | ||
}, | ||
input: { | ||
target: '../specifications/petstore.yaml', | ||
}, | ||
}, | ||
}); |
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