@orval/fetch 7.0.0 → 7.0.1
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/dist/index.d.ts +1 -1
- package/dist/index.js +16 -9
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { GeneratorVerbOptions, GeneratorOptions, ClientBuilder, ClientGeneratorsBuilder } from '@orval/core';
|
|
2
2
|
|
|
3
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;
|
|
4
|
+
declare const fetchResponseTypeName: (includeHttpStatusReturnType: boolean, definitionSuccessResponse: string, operationName: string) => string;
|
|
5
5
|
declare const generateClient: ClientBuilder;
|
|
6
6
|
declare const builder: () => () => ClientGeneratorsBuilder;
|
|
7
7
|
|
package/dist/index.js
CHANGED
|
@@ -66,11 +66,17 @@ ${queryParams ? `
|
|
|
66
66
|
${queryParams ? `return normalizedParams.size ? \`${route}${"?${normalizedParams.toString()}"}\` : \`${route}\`` : `return \`${route}\``}
|
|
67
67
|
}
|
|
68
68
|
`;
|
|
69
|
-
const responseTypeName = fetchResponseTypeName(
|
|
70
|
-
|
|
69
|
+
const responseTypeName = fetchResponseTypeName(
|
|
70
|
+
override.fetch.includeHttpStatusReturnType,
|
|
71
|
+
response.definition.success,
|
|
72
|
+
operationName
|
|
73
|
+
);
|
|
74
|
+
const responseTypeImplementation = override.fetch.includeHttpStatusReturnType ? `export type ${responseTypeName} = {
|
|
71
75
|
data: ${response.definition.success || "unknown"};
|
|
72
76
|
status: number;
|
|
73
|
-
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
` : "";
|
|
74
80
|
const getUrlFnProperties = props.filter(
|
|
75
81
|
(prop) => prop.type === import_core.GetterPropType.PARAM || prop.type === import_core.GetterPropType.QUERY_PARAM || prop.type === import_core.GetterPropType.NAMED_PATH_PARAMS
|
|
76
82
|
).map((param) => {
|
|
@@ -84,7 +90,8 @@ ${queryParams ? `
|
|
|
84
90
|
const retrunType = `Promise<${responseTypeName}>`;
|
|
85
91
|
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()}` : "";
|
|
86
92
|
const fetchMethodOption = `method: '${verb.toUpperCase()}'`;
|
|
87
|
-
const
|
|
93
|
+
const ignoreContentTypes = ["multipart/form-data"];
|
|
94
|
+
const fetchHeadersOption = body.contentType && !ignoreContentTypes.includes(body.contentType) ? `headers: { 'Content-Type': '${body.contentType}' }` : "";
|
|
88
95
|
const requestBodyParams = (0, import_core.generateBodyOptions)(
|
|
89
96
|
body,
|
|
90
97
|
isFormData,
|
|
@@ -103,7 +110,7 @@ ${queryParams ? `
|
|
|
103
110
|
)
|
|
104
111
|
const data = await res.json()
|
|
105
112
|
|
|
106
|
-
return { status: res.status, data }
|
|
113
|
+
${override.fetch.includeHttpStatusReturnType ? "return { status: res.status, data }" : `return data as ${responseTypeName}`}
|
|
107
114
|
`;
|
|
108
115
|
const customFetchResponseImplementation = `return ${mutator == null ? void 0 : mutator.name}<${retrunType}>(${fetchFnOptions});`;
|
|
109
116
|
const bodyForm = (0, import_core.generateFormDataAndUrlEncodedFunction)({
|
|
@@ -118,14 +125,14 @@ ${queryParams ? `
|
|
|
118
125
|
${bodyForm ? ` ${bodyForm}` : ""}
|
|
119
126
|
${fetchImplementationBody}}
|
|
120
127
|
`;
|
|
121
|
-
const implementation = `${responseTypeImplementation}
|
|
122
|
-
|
|
123
|
-
${getUrlFnImplementation}
|
|
128
|
+
const implementation = `${responseTypeImplementation}${getUrlFnImplementation}
|
|
124
129
|
${fetchImplementation}
|
|
125
130
|
`;
|
|
126
131
|
return implementation;
|
|
127
132
|
};
|
|
128
|
-
var fetchResponseTypeName = (operationName) =>
|
|
133
|
+
var fetchResponseTypeName = (includeHttpStatusReturnType, definitionSuccessResponse, operationName) => {
|
|
134
|
+
return includeHttpStatusReturnType ? `${operationName}Response` : definitionSuccessResponse;
|
|
135
|
+
};
|
|
129
136
|
var generateClient = (verbOptions, options) => {
|
|
130
137
|
const imports = (0, import_core.generateVerbImports)(verbOptions);
|
|
131
138
|
const functionImplementation = generateRequestFunction(verbOptions, options);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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}
|
|
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(\n override.fetch.includeHttpStatusReturnType,\n response.definition.success,\n operationName,\n );\n const responseTypeImplementation = override.fetch.includeHttpStatusReturnType\n ? `export type ${responseTypeName} = {\n data: ${response.definition.success || 'unknown'};\n status: number;\n}\\n\\n`\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 ignoreContentTypes = ['multipart/form-data'];\n const fetchHeadersOption =\n body.contentType && !ignoreContentTypes.includes(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 ${override.fetch.includeHttpStatusReturnType ? 'return { status: res.status, data }' : `return data as ${responseTypeName}`}\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 `${getUrlFnImplementation}\\n` +\n `${fetchImplementation}\\n`;\n\n return implementation;\n};\n\nexport const fetchResponseTypeName = (\n includeHttpStatusReturnType: boolean,\n definitionSuccessResponse: string,\n operationName: string,\n) => {\n return includeHttpStatusReturnType\n ? `${operationName}Response`\n : definitionSuccessResponse;\n};\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,2BAAe;AAAA,IACjC;AAAA,IAC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;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": "7.0.
|
|
3
|
+
"version": "7.0.1",
|
|
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": "7.0.
|
|
16
|
+
"@orval/core": "7.0.1"
|
|
17
17
|
}
|
|
18
18
|
}
|