UNPKG

8.58 kBSource Map (JSON)View Raw
1{"version":3,"file":"config.js","names":["DEFAULT_LIMIT","defaultBodyParserOptions","limit","defaultUrlEncoded","extended","defaultConfig","bodyParser","text","raw","json","urlencoded","warnings","bodyParserConfigFailover","property","expectedType","actualFailover","_","original","push","replacedWith","functionConfigSchema","Joi","object","keys","type","string","alternatives","number","default","failover","unknown","boolean","required","createConfig","userConfig","value","validate","config"],"sources":["../../../src/internal-plugins/functions/config.ts"],"sourcesContent":["import Joi from \"joi\"\nimport type {\n GatsbyFunctionBodyParserCommonMiddlewareConfig,\n GatsbyFunctionBodyParserUrlencodedConfig,\n} from \"gatsby\"\n\nconst DEFAULT_LIMIT = `100kb`\n\n// similar to `GatsbyFunctionBodyParserConfig` and `IGatsbyFunctionConfigProcessed`\n// from index.d.ts, just with fields required (not optional).\n// `createConfig()` will fill in defaults\nexport interface IGatsbyBodyParserConfigProcessed {\n json: GatsbyFunctionBodyParserCommonMiddlewareConfig\n raw: GatsbyFunctionBodyParserCommonMiddlewareConfig\n text: GatsbyFunctionBodyParserCommonMiddlewareConfig\n urlencoded: GatsbyFunctionBodyParserUrlencodedConfig\n}\nexport interface IGatsbyFunctionConfigProcessed {\n bodyParser: IGatsbyBodyParserConfigProcessed\n}\n\nconst defaultBodyParserOptions: GatsbyFunctionBodyParserCommonMiddlewareConfig =\n {\n limit: DEFAULT_LIMIT,\n }\nconst defaultUrlEncoded = { extended: true }\nconst defaultConfig = {\n bodyParser: {\n text: defaultBodyParserOptions,\n raw: defaultBodyParserOptions,\n json: defaultBodyParserOptions,\n urlencoded: { ...defaultBodyParserOptions, ...defaultUrlEncoded },\n },\n}\n\nexport interface IAPIFunctionWarning {\n property: string | null\n original: any\n expectedType: string\n replacedWith: any\n}\n\nlet warnings: Array<IAPIFunctionWarning> = []\n\nfunction bodyParserConfigFailover(\n property: keyof IGatsbyBodyParserConfigProcessed,\n expectedType: string\n): any {\n return function actualFailover(_, { original }): any {\n warnings.push({\n property: `bodyParser.${property}`,\n original,\n expectedType,\n replacedWith: defaultConfig.bodyParser[property],\n })\n\n return defaultConfig.bodyParser[property]\n }\n}\n\nconst functionConfigSchema: Joi.ObjectSchema<IGatsbyFunctionConfigProcessed> =\n Joi.object()\n .keys({\n bodyParser: Joi.object()\n .keys({\n json: Joi.object()\n .keys({\n type: Joi.string(),\n limit: Joi.alternatives(Joi.string(), Joi.number()),\n })\n .default(defaultConfig.bodyParser.json)\n .failover(\n bodyParserConfigFailover(\n `json`,\n `{\\n type?: string\\n limit?: string | number\\n}`\n )\n )\n .unknown(false),\n text: Joi.object()\n .keys({\n type: Joi.string(),\n limit: Joi.alternatives(Joi.string(), Joi.number()),\n })\n .unknown(false)\n .default(defaultConfig.bodyParser.text)\n .failover(\n bodyParserConfigFailover(\n `text`,\n `{\\n type?: string\\n limit?: string | number\\n}`\n )\n ),\n raw: Joi.object()\n .keys({\n type: Joi.string(),\n limit: Joi.alternatives(Joi.string(), Joi.number()),\n })\n .unknown(false)\n .default(defaultConfig.bodyParser.raw)\n .failover(\n bodyParserConfigFailover(\n `raw`,\n `{\\n type?: string\\n limit?: string | number\\n}`\n )\n ),\n urlencoded: Joi.object()\n .keys({\n type: Joi.string(),\n limit: Joi.alternatives(Joi.string(), Joi.number()),\n extended: Joi.boolean().required(),\n })\n .unknown(false)\n .default(defaultConfig.bodyParser.urlencoded)\n .failover(\n bodyParserConfigFailover(\n `urlencoded`,\n `{\\n type?: string\\n limit: string | number\\n extended: boolean\\n}`\n )\n ),\n })\n .unknown(false)\n .default(defaultConfig.bodyParser)\n .failover((_, { original }) => {\n warnings.push({\n property: `bodyParser`,\n original,\n expectedType: `{\\n text?: GatsbyFunctionBodyParserCommonMiddlewareConfig\\n json?: GatsbyFunctionBodyParserCommonMiddlewareConfig\\n raw?: GatsbyFunctionBodyParserCommonMiddlewareConfig\\n urlencoded?: GatsbyFunctionBodyParserUrlencodedConfig\\n}`,\n replacedWith: defaultConfig.bodyParser,\n })\n return defaultConfig.bodyParser\n }),\n })\n .unknown(false)\n .default(defaultConfig)\n .failover((_, { original }) => {\n warnings.push({\n property: null,\n original,\n expectedType: `{\\n bodyParser?: GatsbyFunctionBodyParserConfig\\n}`,\n replacedWith: defaultConfig,\n })\n return defaultConfig\n })\n\nexport function createConfig(userConfig: unknown): {\n config: IGatsbyFunctionConfigProcessed\n warnings: Array<IAPIFunctionWarning>\n} {\n warnings = []\n const { value } = functionConfigSchema.validate(userConfig)\n const config = value as IGatsbyFunctionConfigProcessed\n return { config, warnings }\n}\n"],"mappings":";;;;;AAAA;AAMA,MAAMA,aAAa,GAAI,OAAM;;AAE7B;AACA;AACA;;AAWA,MAAMC,wBAAwE,GAC5E;EACEC,KAAK,EAAEF;AACT,CAAC;AACH,MAAMG,iBAAiB,GAAG;EAAEC,QAAQ,EAAE;AAAK,CAAC;AAC5C,MAAMC,aAAa,GAAG;EACpBC,UAAU,EAAE;IACVC,IAAI,EAAEN,wBAAwB;IAC9BO,GAAG,EAAEP,wBAAwB;IAC7BQ,IAAI,EAAER,wBAAwB;IAC9BS,UAAU,EAAE;MAAE,GAAGT,wBAAwB;MAAE,GAAGE;IAAkB;EAClE;AACF,CAAC;AASD,IAAIQ,QAAoC,GAAG,EAAE;AAE7C,SAASC,wBAAwB,CAC/BC,QAAgD,EAChDC,YAAoB,EACf;EACL,OAAO,SAASC,cAAc,CAACC,CAAC,EAAE;IAAEC;EAAS,CAAC,EAAO;IACnDN,QAAQ,CAACO,IAAI,CAAC;MACZL,QAAQ,EAAG,cAAaA,QAAS,EAAC;MAClCI,QAAQ;MACRH,YAAY;MACZK,YAAY,EAAEd,aAAa,CAACC,UAAU,CAACO,QAAQ;IACjD,CAAC,CAAC;IAEF,OAAOR,aAAa,CAACC,UAAU,CAACO,QAAQ,CAAC;EAC3C,CAAC;AACH;AAEA,MAAMO,oBAAsE,GAC1EC,YAAG,CAACC,MAAM,EAAE,CACTC,IAAI,CAAC;EACJjB,UAAU,EAAEe,YAAG,CAACC,MAAM,EAAE,CACr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