UNPKG

@kubb/plugin-client

Version:

API client generator plugin for Kubb, creating type-safe HTTP clients (Axios, Fetch) from OpenAPI specifications for making API requests.

201 lines (199 loc) 8.27 kB
import { URLPath } from '@kubb/core/utils'; import { isOptional } from '@kubb/oas'; import { getComments, getPathParams } from '@kubb/plugin-oas/utils'; import { File, Function, FunctionParams, Const } from '@kubb/react'; import { jsx, jsxs, Fragment } from '@kubb/react/jsx-runtime'; // src/components/Url.tsx function getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }) { if (paramsType === "object") { return FunctionParams.factory({ data: { mode: "object", children: { ...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }) } } }); } return FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: isOptional(typeSchemas.pathParams?.schema) } : void 0 }); } function Url({ name, isExportable = true, isIndexable = true, typeSchemas, baseURL, paramsType, paramsCasing, pathParamsType, operation }) { const path = new URLPath(operation.path, { casing: paramsCasing }); const params = getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }); return /* @__PURE__ */ jsx(File.Source, { name, isExportable, isIndexable, children: /* @__PURE__ */ jsx(Function, { name, export: isExportable, params: params.toConstructor(), children: `return ${path.toTemplateString({ prefix: baseURL })} as const` }) }); } Url.getParams = getParams; function getParams2({ paramsType, paramsCasing, pathParamsType, typeSchemas, isConfigurable }) { if (paramsType === "object") { return FunctionParams.factory({ data: { mode: "object", children: { ...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0 } }, config: isConfigurable ? { type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }", default: "{}" } : void 0 }); } return FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: isOptional(typeSchemas.pathParams?.schema) } : void 0, data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema) } : void 0, config: isConfigurable ? { type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : "Partial<RequestConfig> & { client?: typeof fetch }", default: "{}" } : void 0 }); } function Client({ name, isExportable = true, isIndexable = true, returnType, typeSchemas, baseURL, dataReturnType, parser, zodSchemas, paramsType, paramsCasing, pathParamsType, operation, urlName, children, isConfigurable = true }) { const path = new URLPath(operation.path, { casing: paramsCasing }); const contentType = operation.getContentType(); const isFormData = contentType === "multipart/form-data"; const headers = [ contentType !== "application/json" ? `'Content-Type': '${contentType}'` : void 0, typeSchemas.headerParams?.name ? "...headers" : void 0 ].filter(Boolean); const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`; const generics = [typeSchemas.response.name, TError, typeSchemas.request?.name || "unknown"].filter(Boolean); const params = getParams2({ paramsType, paramsCasing, pathParamsType, typeSchemas, isConfigurable }); const urlParams = Url.getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }); const clientParams = FunctionParams.factory({ config: { mode: "object", children: { method: { value: JSON.stringify(operation.method.toUpperCase()) }, url: { value: urlName ? `${urlName}(${urlParams.toCall()}).toString()` : path.template }, baseURL: baseURL && !urlName ? { value: JSON.stringify(baseURL) } : void 0, params: typeSchemas.queryParams?.name ? {} : void 0, data: typeSchemas.request?.name ? { value: isFormData ? "formData" : "requestData" } : void 0, requestConfig: isConfigurable ? { mode: "inlineSpread" } : void 0, headers: headers.length ? { value: isConfigurable ? `{ ${headers.join(", ")}, ...requestConfig.headers }` : `{ ${headers.join(", ")} }` } : void 0 } } }); const formData = isFormData ? ` const formData = new FormData() if(requestData) { Object.keys(requestData).forEach((key) => { const value = requestData[key as keyof typeof requestData]; if (typeof value === 'string' || (value as unknown) instanceof Blob) { formData.append(key, value as unknown as string | Blob); } }) } ` : ""; const childrenElement = children ? children : /* @__PURE__ */ jsxs(Fragment, { children: [ dataReturnType === "full" && parser === "zod" && zodSchemas && `return {...res, data: ${zodSchemas.response.name}.parse(res.data)}`, dataReturnType === "data" && parser === "zod" && zodSchemas && `return ${zodSchemas.response.name}.parse(res.data)`, dataReturnType === "full" && parser === "client" && "return res", dataReturnType === "data" && parser === "client" && "return res.data" ] }); return /* @__PURE__ */ jsx(File.Source, { name, isExportable, isIndexable, children: /* @__PURE__ */ jsxs( Function, { name, async: true, export: isExportable, params: params.toConstructor(), JSDoc: { comments: getComments(operation) }, returnType, children: [ isConfigurable ? "const { client:request = fetch, ...requestConfig } = config" : "", /* @__PURE__ */ jsx("br", {}), /* @__PURE__ */ jsx("br", {}), parser === "zod" && zodSchemas?.request?.name && `const requestData = ${zodSchemas.request.name}.parse(data)`, parser === "client" && typeSchemas?.request?.name && "const requestData = data", /* @__PURE__ */ jsx("br", {}), formData, isConfigurable ? `const res = await request<${generics.join(", ")}>(${clientParams.toCall()})` : `const res = await fetch<${generics.join(", ")}>(${clientParams.toCall()})`, /* @__PURE__ */ jsx("br", {}), childrenElement ] } ) }); } Client.getParams = getParams2; function Operations({ name, operations }) { const operationsObject = {}; operations.forEach((operation) => { operationsObject[operation.getOperationId()] = { path: new URLPath(operation.path).URL, method: operation.method }; }); return /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Const, { name, export: true, asConst: true, children: JSON.stringify(operationsObject, void 0, 2) }) }); } export { Client, Operations, Url }; //# sourceMappingURL=chunk-LNJCFB5O.js.map //# sourceMappingURL=chunk-LNJCFB5O.js.map