@orval/fetch 6.30.2 → 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 -9
- package/dist/index.d.ts +4 -2
- package/dist/index.js +37 -18
- 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,11 +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/
|
|
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");
|
|
@@ -30,6 +32,7 @@ var generateRequestFunction = ({
|
|
|
30
32
|
queryParams,
|
|
31
33
|
operationName,
|
|
32
34
|
response,
|
|
35
|
+
mutator,
|
|
33
36
|
body,
|
|
34
37
|
props,
|
|
35
38
|
verb,
|
|
@@ -60,9 +63,14 @@ ${queryParams ? `
|
|
|
60
63
|
}
|
|
61
64
|
});` : ""}
|
|
62
65
|
|
|
63
|
-
return \`${route}${
|
|
66
|
+
${queryParams ? `return normalizedParams.size ? \`${route}${"?${normalizedParams.toString()}"}\` : \`${route}\`` : `return \`${route}\``}
|
|
64
67
|
}
|
|
65
68
|
`;
|
|
69
|
+
const responseTypeName = fetchResponseTypeName(operationName);
|
|
70
|
+
const responseTypeImplementation = `export type ${responseTypeName} = {
|
|
71
|
+
data: ${response.definition.success || "unknown"};
|
|
72
|
+
status: number;
|
|
73
|
+
}`;
|
|
66
74
|
const getUrlFnProperties = props.filter(
|
|
67
75
|
(prop) => prop.type === import_core.GetterPropType.PARAM || prop.type === import_core.GetterPropType.QUERY_PARAM || prop.type === import_core.GetterPropType.NAMED_PATH_PARAMS
|
|
68
76
|
).map((param) => {
|
|
@@ -73,26 +81,31 @@ ${queryParams ? `
|
|
|
73
81
|
}
|
|
74
82
|
}).join(",");
|
|
75
83
|
const args = `${(0, import_core.toObjectString)(props, "implementation")} ${isRequestOptions ? `options?: RequestInit` : ""}`;
|
|
76
|
-
const retrunType = `Promise<${
|
|
84
|
+
const retrunType = `Promise<${responseTypeName}>`;
|
|
77
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()}` : "";
|
|
78
86
|
const fetchMethodOption = `method: '${verb.toUpperCase()}'`;
|
|
87
|
+
const fetchHeadersOption = body.contentType ? `headers: { 'Content-Type': '${body.contentType}' }` : "";
|
|
79
88
|
const requestBodyParams = (0, import_core.generateBodyOptions)(
|
|
80
89
|
body,
|
|
81
90
|
isFormData,
|
|
82
91
|
isFormUrlEncoded
|
|
83
92
|
);
|
|
84
|
-
const fetchBodyOption = requestBodyParams ? `body: JSON.stringify(${requestBodyParams})` : "";
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
93
|
+
const fetchBodyOption = requestBodyParams ? isFormData && body.formData || isFormUrlEncoded && body.formUrlEncoded ? `body: ${requestBodyParams}` : `body: JSON.stringify(${requestBodyParams})` : "";
|
|
94
|
+
const fetchFnOptions = `${getUrlFnName}(${getUrlFnProperties}),
|
|
95
|
+
{${globalFetchOptions ? "\n" : ""} ${globalFetchOptions}
|
|
96
|
+
${isRequestOptions ? "...options," : ""}
|
|
97
|
+
${fetchMethodOption}${fetchHeadersOption ? "," : ""}
|
|
98
|
+
${fetchHeadersOption}${fetchBodyOption ? "," : ""}
|
|
99
|
+
${fetchBodyOption}
|
|
100
|
+
}
|
|
101
|
+
`;
|
|
102
|
+
const fetchResponseImplementation = `const res = await fetch(${fetchFnOptions}
|
|
92
103
|
)
|
|
93
|
-
|
|
94
|
-
|
|
104
|
+
const data = await res.json()
|
|
105
|
+
|
|
106
|
+
return { status: res.status, data }
|
|
95
107
|
`;
|
|
108
|
+
const customFetchResponseImplementation = `return ${mutator == null ? void 0 : mutator.name}<${retrunType}>(${fetchFnOptions});`;
|
|
96
109
|
const bodyForm = (0, import_core.generateFormDataAndUrlEncodedFunction)({
|
|
97
110
|
formData,
|
|
98
111
|
formUrlEncoded,
|
|
@@ -100,15 +113,19 @@ ${queryParams ? `
|
|
|
100
113
|
isFormData,
|
|
101
114
|
isFormUrlEncoded
|
|
102
115
|
});
|
|
103
|
-
const fetchImplementationBody =
|
|
104
|
-
` : ""} ${fetchResponseImplementation}`;
|
|
116
|
+
const fetchImplementationBody = mutator ? customFetchResponseImplementation : fetchResponseImplementation;
|
|
105
117
|
const fetchImplementation = `export const ${operationName} = async (${args}): ${retrunType} => {
|
|
106
|
-
${
|
|
107
|
-
|
|
118
|
+
${bodyForm ? ` ${bodyForm}` : ""}
|
|
119
|
+
${fetchImplementationBody}}
|
|
120
|
+
`;
|
|
121
|
+
const implementation = `${responseTypeImplementation}
|
|
122
|
+
|
|
123
|
+
${getUrlFnImplementation}
|
|
108
124
|
${fetchImplementation}
|
|
109
125
|
`;
|
|
110
126
|
return implementation;
|
|
111
127
|
};
|
|
128
|
+
var fetchResponseTypeName = (operationName) => `${operationName}Response`;
|
|
112
129
|
var generateClient = (verbOptions, options) => {
|
|
113
130
|
const imports = (0, import_core.generateVerbImports)(verbOptions);
|
|
114
131
|
const functionImplementation = generateRequestFunction(verbOptions, options);
|
|
@@ -127,6 +144,8 @@ var src_default = builder;
|
|
|
127
144
|
// Annotate the CommonJS export names for ESM import in node:
|
|
128
145
|
0 && (module.exports = {
|
|
129
146
|
builder,
|
|
130
|
-
|
|
147
|
+
fetchResponseTypeName,
|
|
148
|
+
generateClient,
|
|
149
|
+
generateRequestFunction
|
|
131
150
|
});
|
|
132
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;AAAA;AAAA;AAAA,kBAaO;AAEA,IAAM,0BAA0B,CACrC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GACA,EAAE,MAAM,MACL;AA7BL;AA8BE,QAAM,oBAAmB,qCAAU,oBAAmB;AACtD,QAAM,cAAa,qCAAU,cAAa;AAC1C,QAAM,oBAAmB,qCAAU,oBAAmB;AAEtD,QAAM,mBAAe,mBAAM,OAAO,aAAa,MAAM;AACrD,QAAM,oBAAgB;AAAA,IACpB,MAAM;AAAA,MACJ,CAAC,SACC,KAAK,SAAS,2BAAe,SAC7B,KAAK,SAAS,2BAAe,qBAC7B,KAAK,SAAS,2B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|
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
|
}
|