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.

205 lines (202 loc) 8.54 kB
'use strict'; var utils = require('@kubb/core/utils'); var oas = require('@kubb/oas'); var utils$1 = require('@kubb/plugin-oas/utils'); var react = require('@kubb/react'); var jsxRuntime = require('@kubb/react/jsx-runtime'); // src/components/Url.tsx function getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }) { if (paramsType === "object") { return react.FunctionParams.factory({ data: { mode: "object", children: { ...utils$1.getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }) } } }); } return react.FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: utils$1.getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: oas.isOptional(typeSchemas.pathParams?.schema) } : void 0 }); } function Url({ name, isExportable = true, isIndexable = true, typeSchemas, baseURL, paramsType, paramsCasing, pathParamsType, operation }) { const path = new utils.URLPath(operation.path, { casing: paramsCasing }); const params = getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }); return /* @__PURE__ */ jsxRuntime.jsx(react.File.Source, { name, isExportable, isIndexable, children: /* @__PURE__ */ jsxRuntime.jsx(react.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 react.FunctionParams.factory({ data: { mode: "object", children: { ...utils$1.getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: oas.isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: oas.isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: oas.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 react.FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === "object" ? "object" : "inlineSpread", children: utils$1.getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: oas.isOptional(typeSchemas.pathParams?.schema) } : void 0, data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: oas.isOptional(typeSchemas.request?.schema) } : void 0, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: oas.isOptional(typeSchemas.queryParams?.schema) } : void 0, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: oas.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 utils.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 = react.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__ */ jsxRuntime.jsxs(jsxRuntime.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__ */ jsxRuntime.jsx(react.File.Source, { name, isExportable, isIndexable, children: /* @__PURE__ */ jsxRuntime.jsxs( react.Function, { name, async: true, export: isExportable, params: params.toConstructor(), JSDoc: { comments: utils$1.getComments(operation) }, returnType, children: [ isConfigurable ? "const { client:request = fetch, ...requestConfig } = config" : "", /* @__PURE__ */ jsxRuntime.jsx("br", {}), /* @__PURE__ */ jsxRuntime.jsx("br", {}), parser === "zod" && zodSchemas?.request?.name && `const requestData = ${zodSchemas.request.name}.parse(data)`, parser === "client" && typeSchemas?.request?.name && "const requestData = data", /* @__PURE__ */ jsxRuntime.jsx("br", {}), formData, isConfigurable ? `const res = await request<${generics.join(", ")}>(${clientParams.toCall()})` : `const res = await fetch<${generics.join(", ")}>(${clientParams.toCall()})`, /* @__PURE__ */ jsxRuntime.jsx("br", {}), childrenElement ] } ) }); } Client.getParams = getParams2; function Operations({ name, operations }) { const operationsObject = {}; operations.forEach((operation) => { operationsObject[operation.getOperationId()] = { path: new utils.URLPath(operation.path).URL, method: operation.method }; }); return /* @__PURE__ */ jsxRuntime.jsx(react.File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsxRuntime.jsx(react.Const, { name, export: true, asConst: true, children: JSON.stringify(operationsObject, void 0, 2) }) }); } exports.Client = Client; exports.Operations = Operations; exports.Url = Url; //# sourceMappingURL=chunk-H5KNP3DD.cjs.map //# sourceMappingURL=chunk-H5KNP3DD.cjs.map