@orval/fetch 6.31.0 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -10
- package/dist/index.d.ts +4 -2
- package/dist/index.js +18 -10
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[](https://badge.fury.io/js/orval)
|
|
2
2
|
[](https://opensource.org/licenses/MIT)
|
|
3
|
-
[](https://github.com/orval-labs/orval/actions/workflows/tests.yaml)
|
|
4
4
|
|
|
5
5
|
<p align="center">
|
|
6
6
|
<img src="./logo/orval-logo-horizontal.svg?raw=true" width="500" height="160" alt="orval - Restfull Client Generator" />
|
|
@@ -19,12 +19,12 @@
|
|
|
19
19
|
|
|
20
20
|
You can find below some samples
|
|
21
21
|
|
|
22
|
-
- [react app](https://github.com/
|
|
23
|
-
- [react query](https://github.com/
|
|
24
|
-
- [svelte query](https://github.com/
|
|
25
|
-
- [vue query](https://github.com/
|
|
26
|
-
- [react app with swr](https://github.com/
|
|
27
|
-
- [nx fastify react](https://github.com/
|
|
28
|
-
- [angular app](https://github.com/
|
|
29
|
-
- [hono](https://github.com/
|
|
30
|
-
- [next app with fetch](https://github.com/
|
|
22
|
+
- [react app](https://github.com/orval-labs/orval/tree/master/samples/react-app)
|
|
23
|
+
- [react query](https://github.com/orval-labs/orval/tree/master/samples/react-query)
|
|
24
|
+
- [svelte query](https://github.com/orval-labs/orval/tree/master/samples/svelte-query)
|
|
25
|
+
- [vue query](https://github.com/orval-labs/orval/tree/master/samples/vue-query)
|
|
26
|
+
- [react app with swr](https://github.com/orval-labs/orval/tree/master/samples/react-app-with-swr)
|
|
27
|
+
- [nx fastify react](https://github.com/orval-labs/orval/tree/master/samples/nx-fastify-react)
|
|
28
|
+
- [angular app](https://github.com/orval-labs/orval/tree/master/samples/angular-app)
|
|
29
|
+
- [hono](https://github.com/orval-labs/orval/tree/master/samples/hono)
|
|
30
|
+
- [next app with fetch](https://github.com/orval-labs/orval/tree/master/samples/next-app-with-fetch)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { ClientBuilder, ClientGeneratorsBuilder } from '@orval/core';
|
|
1
|
+
import { GeneratorVerbOptions, GeneratorOptions, ClientBuilder, ClientGeneratorsBuilder } from '@orval/core';
|
|
2
2
|
|
|
3
|
+
declare const generateRequestFunction: ({ queryParams, operationName, response, mutator, body, props, verb, formData, formUrlEncoded, override, }: GeneratorVerbOptions, { route }: GeneratorOptions) => string;
|
|
4
|
+
declare const fetchResponseTypeName: (operationName: string) => string;
|
|
3
5
|
declare const generateClient: ClientBuilder;
|
|
4
6
|
declare const builder: () => () => ClientGeneratorsBuilder;
|
|
5
7
|
|
|
6
|
-
export { builder, builder as default, generateClient };
|
|
8
|
+
export { builder, builder as default, fetchResponseTypeName, generateClient, generateRequestFunction };
|
package/dist/index.js
CHANGED
|
@@ -22,7 +22,9 @@ var src_exports = {};
|
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
builder: () => builder,
|
|
24
24
|
default: () => src_default,
|
|
25
|
-
|
|
25
|
+
fetchResponseTypeName: () => fetchResponseTypeName,
|
|
26
|
+
generateClient: () => generateClient,
|
|
27
|
+
generateRequestFunction: () => generateRequestFunction
|
|
26
28
|
});
|
|
27
29
|
module.exports = __toCommonJS(src_exports);
|
|
28
30
|
var import_core = require("@orval/core");
|
|
@@ -61,11 +63,11 @@ ${queryParams ? `
|
|
|
61
63
|
}
|
|
62
64
|
});` : ""}
|
|
63
65
|
|
|
64
|
-
return \`${route}${
|
|
66
|
+
${queryParams ? `return normalizedParams.size ? \`${route}${"?${normalizedParams.toString()}"}\` : \`${route}\`` : `return \`${route}\``}
|
|
65
67
|
}
|
|
66
68
|
`;
|
|
67
|
-
const responseTypeName =
|
|
68
|
-
const responseTypeImplementation = `export type ${
|
|
69
|
+
const responseTypeName = fetchResponseTypeName(operationName);
|
|
70
|
+
const responseTypeImplementation = `export type ${responseTypeName} = {
|
|
69
71
|
data: ${response.definition.success || "unknown"};
|
|
70
72
|
status: number;
|
|
71
73
|
}`;
|
|
@@ -82,16 +84,18 @@ ${queryParams ? `
|
|
|
82
84
|
const retrunType = `Promise<${responseTypeName}>`;
|
|
83
85
|
const globalFetchOptions = (0, import_core.isObject)(override == null ? void 0 : override.requestOptions) ? `${(_b = (_a = (0, import_core.stringify)(override == null ? void 0 : override.requestOptions)) == null ? void 0 : _a.slice(1, -1)) == null ? void 0 : _b.trim()}` : "";
|
|
84
86
|
const fetchMethodOption = `method: '${verb.toUpperCase()}'`;
|
|
87
|
+
const fetchHeadersOption = body.contentType ? `headers: { 'Content-Type': '${body.contentType}' }` : "";
|
|
85
88
|
const requestBodyParams = (0, import_core.generateBodyOptions)(
|
|
86
89
|
body,
|
|
87
90
|
isFormData,
|
|
88
91
|
isFormUrlEncoded
|
|
89
92
|
);
|
|
90
|
-
const fetchBodyOption = requestBodyParams ? `body: JSON.stringify(${requestBodyParams})` : "";
|
|
93
|
+
const fetchBodyOption = requestBodyParams ? isFormData && body.formData || isFormUrlEncoded && body.formUrlEncoded ? `body: ${requestBodyParams}` : `body: JSON.stringify(${requestBodyParams})` : "";
|
|
91
94
|
const fetchFnOptions = `${getUrlFnName}(${getUrlFnProperties}),
|
|
92
95
|
{${globalFetchOptions ? "\n" : ""} ${globalFetchOptions}
|
|
93
96
|
${isRequestOptions ? "...options," : ""}
|
|
94
|
-
${fetchMethodOption}${
|
|
97
|
+
${fetchMethodOption}${fetchHeadersOption ? "," : ""}
|
|
98
|
+
${fetchHeadersOption}${fetchBodyOption ? "," : ""}
|
|
95
99
|
${fetchBodyOption}
|
|
96
100
|
}
|
|
97
101
|
`;
|
|
@@ -109,10 +113,11 @@ ${queryParams ? `
|
|
|
109
113
|
isFormData,
|
|
110
114
|
isFormUrlEncoded
|
|
111
115
|
});
|
|
112
|
-
const fetchImplementationBody = mutator ? customFetchResponseImplementation :
|
|
113
|
-
` : ""} ${fetchResponseImplementation}`;
|
|
116
|
+
const fetchImplementationBody = mutator ? customFetchResponseImplementation : fetchResponseImplementation;
|
|
114
117
|
const fetchImplementation = `export const ${operationName} = async (${args}): ${retrunType} => {
|
|
115
|
-
${
|
|
118
|
+
${bodyForm ? ` ${bodyForm}` : ""}
|
|
119
|
+
${fetchImplementationBody}}
|
|
120
|
+
`;
|
|
116
121
|
const implementation = `${responseTypeImplementation}
|
|
117
122
|
|
|
118
123
|
${getUrlFnImplementation}
|
|
@@ -120,6 +125,7 @@ ${fetchImplementation}
|
|
|
120
125
|
`;
|
|
121
126
|
return implementation;
|
|
122
127
|
};
|
|
128
|
+
var fetchResponseTypeName = (operationName) => `${operationName}Response`;
|
|
123
129
|
var generateClient = (verbOptions, options) => {
|
|
124
130
|
const imports = (0, import_core.generateVerbImports)(verbOptions);
|
|
125
131
|
const functionImplementation = generateRequestFunction(verbOptions, options);
|
|
@@ -138,6 +144,8 @@ var src_default = builder;
|
|
|
138
144
|
// Annotate the CommonJS export names for ESM import in node:
|
|
139
145
|
0 && (module.exports = {
|
|
140
146
|
builder,
|
|
141
|
-
|
|
147
|
+
fetchResponseTypeName,
|
|
148
|
+
generateClient,
|
|
149
|
+
generateRequestFunction
|
|
142
150
|
});
|
|
143
151
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n camel,\n ClientBuilder,\n
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n camel,\n ClientBuilder,\n ClientGeneratorsBuilder,\n generateFormDataAndUrlEncodedFunction,\n generateVerbImports,\n GeneratorOptions,\n GeneratorVerbOptions,\n GetterPropType,\n stringify,\n toObjectString,\n generateBodyOptions,\n isObject,\n} from '@orval/core';\n\nexport const generateRequestFunction = (\n {\n queryParams,\n operationName,\n response,\n mutator,\n body,\n props,\n verb,\n formData,\n formUrlEncoded,\n override,\n }: GeneratorVerbOptions,\n { route }: GeneratorOptions,\n) => {\n const isRequestOptions = override?.requestOptions !== false;\n const isFormData = override?.formData !== false;\n const isFormUrlEncoded = override?.formUrlEncoded !== false;\n\n const getUrlFnName = camel(`get-${operationName}-url`);\n const getUrlFnProps = toObjectString(\n props.filter(\n (prop) =>\n prop.type === GetterPropType.PARAM ||\n prop.type === GetterPropType.NAMED_PATH_PARAMS ||\n prop.type === GetterPropType.QUERY_PARAM,\n ),\n 'implementation',\n );\n const getUrlFnImplementation = `export const ${getUrlFnName} = (${getUrlFnProps}) => {\n${\n queryParams\n ? `\n const normalizedParams = new URLSearchParams();\n\n Object.entries(params || {}).forEach(([key, value]) => {\n if (value === null) {\n normalizedParams.append(key, 'null');\n } else if (value !== undefined) {\n normalizedParams.append(key, value.toString());\n }\n });`\n : ''\n}\n\n ${\n queryParams\n ? `return normalizedParams.size ? \\`${route}${'?${normalizedParams.toString()}'}\\` : \\`${route}\\``\n : `return \\`${route}\\``\n }\n}\\n`;\n\n const responseTypeName = fetchResponseTypeName(operationName);\n const responseTypeImplementation = `export type ${responseTypeName} = {\n data: ${response.definition.success || 'unknown'};\n status: number;\n}`;\n\n const getUrlFnProperties = props\n .filter(\n (prop) =>\n prop.type === GetterPropType.PARAM ||\n prop.type === GetterPropType.QUERY_PARAM ||\n prop.type === GetterPropType.NAMED_PATH_PARAMS,\n )\n .map((param) => {\n if (param.type === GetterPropType.NAMED_PATH_PARAMS) {\n return param.destructured;\n } else {\n return param.name;\n }\n })\n .join(',');\n\n const args = `${toObjectString(props, 'implementation')} ${isRequestOptions ? `options?: RequestInit` : ''}`;\n const retrunType = `Promise<${responseTypeName}>`;\n\n const globalFetchOptions = isObject(override?.requestOptions)\n ? `${stringify(override?.requestOptions)?.slice(1, -1)?.trim()}`\n : '';\n const fetchMethodOption = `method: '${verb.toUpperCase()}'`;\n const fetchHeadersOption = body.contentType\n ? `headers: { 'Content-Type': '${body.contentType}' }`\n : '';\n const requestBodyParams = generateBodyOptions(\n body,\n isFormData,\n isFormUrlEncoded,\n );\n const fetchBodyOption = requestBodyParams\n ? (isFormData && body.formData) || (isFormUrlEncoded && body.formUrlEncoded)\n ? `body: ${requestBodyParams}`\n : `body: JSON.stringify(${requestBodyParams})`\n : '';\n\n const fetchFnOptions = `${getUrlFnName}(${getUrlFnProperties}),\n {${globalFetchOptions ? '\\n' : ''} ${globalFetchOptions}\n ${isRequestOptions ? '...options,' : ''}\n ${fetchMethodOption}${fetchHeadersOption ? ',' : ''}\n ${fetchHeadersOption}${fetchBodyOption ? ',' : ''}\n ${fetchBodyOption}\n }\n`;\n const fetchResponseImplementation = `const res = await fetch(${fetchFnOptions}\n )\n const data = await res.json()\n\n return { status: res.status, data }\n`;\n const customFetchResponseImplementation = `return ${mutator?.name}<${retrunType}>(${fetchFnOptions});`;\n\n const bodyForm = generateFormDataAndUrlEncodedFunction({\n formData,\n formUrlEncoded,\n body,\n isFormData,\n isFormUrlEncoded,\n });\n\n const fetchImplementationBody = mutator\n ? customFetchResponseImplementation\n : fetchResponseImplementation;\n\n const fetchImplementation = `export const ${operationName} = async (${args}): ${retrunType} => {\n ${bodyForm ? ` ${bodyForm}` : ''}\n ${fetchImplementationBody}}\n`;\n\n const implementation =\n `${responseTypeImplementation}\\n\\n` +\n `${getUrlFnImplementation}\\n` +\n `${fetchImplementation}\\n`;\n\n return implementation;\n};\n\nexport const fetchResponseTypeName = (operationName: string) =>\n `${operationName}Response`;\n\nexport const generateClient: ClientBuilder = (verbOptions, options) => {\n const imports = generateVerbImports(verbOptions);\n const functionImplementation = generateRequestFunction(verbOptions, options);\n\n return {\n implementation: `${functionImplementation}\\n`,\n imports,\n };\n};\n\nconst fetchClientBuilder: ClientGeneratorsBuilder = {\n client: generateClient,\n dependencies: () => [],\n};\n\nexport const builder = () => () => fetchClientBuilder;\n\nexport default builder;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;A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BACpC,GAAG,aAAa;AAEX,IAAM,iBAAgC,CAAC,aAAa,YAAY;AACrE,QAAM,cAAU,iCAAoB,WAAW;AAC/C,QAAM,yBAAyB,wBAAwB,aAAa,OAAO;AAE3E,SAAO;AAAA,IACL,gBAAgB,GAAG,sBAAsB;AAAA;AAAA,IACzC;AAAA,EACF;AACF;AAEA,IAAM,qBAA8C;AAAA,EAClD,QAAQ;AAAA,EACR,cAAc,MAAM,CAAC;AACvB;AAEO,IAAM,UAAU,MAAM,MAAM;AAEnC,IAAO,cAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orval/fetch",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -13,6 +13,6 @@
|
|
|
13
13
|
"lint": "eslint src/**/*.ts"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@orval/core": "
|
|
16
|
+
"@orval/core": "7.0.0"
|
|
17
17
|
}
|
|
18
18
|
}
|