{"version":3,"file":"index.modern.mjs","sources":["../../src/koa-api.ts"],"sourcesContent":["import Router from '@koa/router'\nimport Koa from 'koa'\nimport type { NextApiRequest, NextApiResponse } from 'next'\nimport onFinished from 'on-finished'\n\ndeclare module 'koa' {\n  type IncomingMessage = NextApiRequest\n  type ServerResponse = NextApiResponse\n  interface Request extends Koa.BaseRequest {\n    body?: unknown\n  }\n}\n\nexport { Router }\nexport { Koa }\n\nexport type KoaOptions = {\n  /** defaulting to the NODE_ENV or \"development\"*/\n  env?: string\n  /**  array of signed cookie keys*/\n  keys?: string[]\n  /** when true proxy header fields will be trusted*/\n  proxy?: boolean\n  /** offset of .subdomains to ignore, default to 2*/\n  subdomainOffset?: number\n  /** proxy ip header, default to X-Forwarded-For*/\n  proxyIpHeader?: string\n  /** max ips read from proxy ip header, default to 0 (means infinity)*/\n  maxIpsCount?: number\n}\n\n/** Koa API options*/\nexport type KoaApiOptions = {\n  /** original koa options*/\n  koa?: KoaOptions\n  /** if parsed body from Nextjs request should be attached to ctx.request */\n  attachBody?: boolean\n  /** {@link Router.RouterOptions}*/\n  router?: Router.RouterOptions\n  /** {@link Router.RouterAllowedMethodsOptions | Router.RouterAllowedMethodsOptions}*/\n  routerAllowedMethods?: Router.RouterAllowedMethodsOptions\n}\n\nexport class KoaApi<\n  TState extends Koa.DefaultState = Koa.DefaultState,\n  TContext extends Koa.DefaultContext = Koa.DefaultContext\n> extends Koa<TState, TContext> {\n  router: Router<TState, TContext>\n\n  protected firstRun = true\n\n  protected options: KoaApiOptions\n\n  constructor(options: KoaApiOptions = {}) {\n    super(options.koa)\n\n    this.options = {\n      ...options,\n      attachBody: options?.attachBody ?? true\n    }\n\n    this.router = new Router(this.options.router)\n\n    if (this.options.attachBody) {\n      this.use(async (ctx, next) => {\n        ctx.request.body = ctx.req.body\n\n        return next()\n      })\n    }\n  }\n\n  /**\n   * Run koa api with request and response objects\n   * @param req - nextjs request\n   * @param res - nextjs response\n   */\n  async run(req: NextApiRequest, res: NextApiResponse) {\n    const p = new Promise((resolve) => onFinished(res, resolve))\n\n    if (this.firstRun) {\n      this.firstRun = false\n      this.use(this.router.routes()).use(\n        this.router.allowedMethods(this.options.routerAllowedMethods)\n      )\n    }\n\n    this.callback()(req, res) as unknown as Promise<void>\n\n    return p\n  }\n}\n\n/**\n * Helper function for Next.js api routes that automatically runs the KoaApi.\n * It should be used as default export from the Next.js api route file\n * @param koa - KoaApi to run\n */\nexport function withKoaApi(koa: KoaApi<any, any>) {\n  return (req: NextApiRequest, res: NextApiResponse) => koa.run(req, res)\n}\n\n/**\n * Attaches router to a particular path. Internally it setups a prefix on the router, and calls `router.routes()` and `router.allowedMethods()`\n * @param path - path to attach the router to\n * @param router - router to attach\n */\nexport function attachRouter(\n  path: string,\n  app: KoaApi<any, any>,\n  router: Router<any, any>\n) {\n  app.use(router.prefix(path).routes()).use(router.allowedMethods())\n}\n"],"names":["KoaApi","Koa","constructor","options","koa","router","firstRun","attachBody","Router","use","ctx","next","request","body","req","run","res","p","Promise","resolve","onFinished","routes","allowedMethods","routerAllowedMethods","callback","withKoaApi","attachRouter","path","app","prefix"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;MA2CaA,eAGHC;EAORC,YAAYC,UAAyB;;;IACnC,MAAMA,OAAO,CAACC,GAAd;SAPFC;SAEUC,WAAW;SAEXH;IAKR,KAAKA,OAAL,gBACKA,OADL;MAEEI,UAAU,yBAAEJ,OAAF,oBAAEA,OAAO,CAAEI,UAAX,kCAAyB;;IAGrC,KAAKF,MAAL,GAAc,IAAIG,MAAJ,CAAW,KAAKL,OAAL,CAAaE,MAAxB,CAAd;;IAEA,IAAI,KAAKF,OAAL,CAAaI,UAAjB,EAA6B;MAC3B,KAAKE,GAAL,CAAS,gBAAOC,GAAP,EAAYC,IAAZ;QACPD,GAAG,CAACE,OAAJ,CAAYC,IAAZ,GAAmBH,GAAG,CAACI,GAAJ,CAAQD,IAA3B;QAEA,OAAOF,IAAI,EAAX;OAHF;;;;;;;;;;EAa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