UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 4.07 kB
{"version":3,"file":"parameters.mjs","names":["constants"],"sources":["../../../../src/libs/http/openapi/parameters.ts"],"sourcesContent":["import type { DescribeRouteOptions } from \"hono-openapi\";\nimport type { OpenAPIV3 } from \"openapi-types\";\nimport z, { type ZodType } from \"zod\";\nimport constants from \"../../../constants/constants.js\";\nimport { translate } from \"../../i18n/index.js\";\n\n/**\n * Used to construct parameters JSON schema for OpenAPI.\n */\nconst parameters = (props: {\n\theaders?: {\n\t\t// Undefined omits the header, boolean controls whether it is required.\n\t\tcsrf?: boolean;\n\t\tauthorization?: boolean;\n\t\tidempotencyKey?: boolean;\n\t};\n\tparams?: ZodType;\n\tquery?: ZodType;\n}) => {\n\tconst routeParameters: DescribeRouteOptions[\"parameters\"] = [];\n\n\tif (props.headers?.csrf !== undefined) {\n\t\trouteParameters.push({\n\t\t\tin: \"header\",\n\t\t\tname: constants.headers.csrf,\n\t\t\trequired: props.headers.csrf,\n\t\t\tdescription: translate(\"server:core.openapi.csrf.header.description\"),\n\t\t\tschema: {\n\t\t\t\ttype: \"string\",\n\t\t\t},\n\t\t});\n\t}\n\tif (props.headers?.idempotencyKey !== undefined) {\n\t\trouteParameters.push({\n\t\t\tin: \"header\",\n\t\t\tname: constants.headers.idempotencyKey,\n\t\t\trequired: props.headers.idempotencyKey,\n\t\t\tdescription: translate(\n\t\t\t\t\"server:core.openapi.idempotency.key.header.description\",\n\t\t\t),\n\t\t\tschema: {\n\t\t\t\ttype: \"string\",\n\t\t\t},\n\t\t});\n\t}\n\tif (props.headers?.authorization !== undefined) {\n\t\trouteParameters.push({\n\t\t\tin: \"header\",\n\t\t\tname: \"Authorization\",\n\t\t\trequired: props.headers.authorization,\n\t\t\tdescription: translate(\n\t\t\t\t\"server:core.openapi.authorization.header.description\",\n\t\t\t),\n\t\t\tschema: {\n\t\t\t\ttype: \"string\",\n\t\t\t},\n\t\t});\n\t}\n\n\tif (props.params) {\n\t\tconst paramsSchema = z.toJSONSchema(props.params) as OpenAPIV3.SchemaObject;\n\t\tif (paramsSchema.properties) {\n\t\t\tfor (const [paramName, paramSchema] of Object.entries(\n\t\t\t\tparamsSchema.properties,\n\t\t\t)) {\n\t\t\t\tconst schema = paramSchema as OpenAPIV3.SchemaObject;\n\t\t\t\trouteParameters.push({\n\t\t\t\t\tname: paramName,\n\t\t\t\t\tin: \"path\",\n\t\t\t\t\trequired: true,\n\t\t\t\t\tdescription: schema.description,\n\t\t\t\t\texample: schema.example,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\tif (props.query) {\n\t\tconst querySchema = z.toJSONSchema(props.query) as OpenAPIV3.SchemaObject;\n\t\tif (querySchema.properties) {\n\t\t\tfor (const [paramName, paramSchema] of Object.entries(\n\t\t\t\tquerySchema.properties,\n\t\t\t)) {\n\t\t\t\tconst schema = paramSchema as OpenAPIV3.SchemaObject;\n\t\t\t\trouteParameters.push({\n\t\t\t\t\tname: paramName,\n\t\t\t\t\tin: \"query\",\n\t\t\t\t\trequired: querySchema.required?.includes(paramName),\n\t\t\t\t\tdescription: schema.description,\n\t\t\t\t\texample: schema.example,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\treturn routeParameters;\n};\n\nexport default parameters;\n"],"mappings":"wHASA,MAAM,EAAc,GASd,CACL,IAAM,EAAsD,CAAC,EAwC7D,GAtCI,EAAM,SAAS,OAAS,IAAA,IAC3B,EAAgB,KAAK,CACpB,GAAI,SACJ,KAAMA,EAAU,QAAQ,KACxB,SAAU,EAAM,QAAQ,KACxB,YAAa,EAAU,6CAA6C,EACpE,OAAQ,CACP,KAAM,QACP,CACD,CAAC,EAEE,EAAM,SAAS,iBAAmB,IAAA,IACrC,EAAgB,KAAK,CACpB,GAAI,SACJ,KAAMA,EAAU,QAAQ,eACxB,SAAU,EAAM,QAAQ,eACxB,YAAa,EACZ,wDACD,EACA,OAAQ,CACP,KAAM,QACP,C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