@routar/fetch 0.1.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -8,9 +8,9 @@ function createFetchExecutor(baseURL, options) {
8
8
  async ({ method, url, params, body, headers, signal }) => {
9
9
  const fullURL = new URL(baseURL.replace(/\/$/, "") + url);
10
10
  if (params) {
11
- core.serializeParams(params).forEach(
12
- (v, k) => fullURL.searchParams.set(k, v)
13
- );
11
+ core.serializeParams(params).forEach((v, k) => {
12
+ fullURL.searchParams.set(k, v);
13
+ });
14
14
  }
15
15
  const defaultHeaders = await options?.defaultHeaders?.() ?? {};
16
16
  const res = await fetch(fullURL.toString(), {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/create-fetch-executor.ts"],"names":["createExecutor","serializeParams"],"mappings":";;;;;AA8BO,SAAS,mBAAA,CACd,SACA,OAAA,EAMU;AACV,EAAA,OAAOA,mBAAA;AAAA,IACL,OAAO,EAAE,MAAA,EAAQ,GAAA,EAAK,QAAQ,IAAA,EAAM,OAAA,EAAS,QAAO,KAAM;AACxD,MAAA,MAAM,OAAA,GAAU,IAAI,GAAA,CAAI,OAAA,CAAQ,QAAQ,KAAA,EAAO,EAAE,IAAI,GAAG,CAAA;AACxD,MAAA,IAAI,MAAA,EAAQ;AACV,QAAAC,oBAAA,CAAgB,MAAM,CAAA,CAAE,OAAA;AAAA,UAAQ,CAAC,CAAA,EAAG,CAAA,KAClC,QAAQ,YAAA,CAAa,GAAA,CAAI,GAAG,CAAC;AAAA,SAC/B;AAAA,MACF;AAEA,MAAA,MAAM,cAAA,GAAkB,MAAM,OAAA,EAAS,cAAA,QAAuB,EAAC;AAE/D,MAAA,MAAM,GAAA,GAAM,MAAM,KAAA,CAAM,OAAA,CAAQ,UAAS,EAAG;AAAA,QAC1C,MAAA;AAAA,QACA,OAAA,EAAS;AAAA,UACP,GAAI,IAAA,IAAQ,IAAA,GAAO,EAAE,cAAA,EAAgB,kBAAA,KAAuB,EAAC;AAAA,UAC7D,GAAG,cAAA;AAAA,UACH,GAAG;AAAA,SACL;AAAA,QACA,MAAM,IAAA,IAAQ,IAAA,GAAO,IAAA,CAAK,SAAA,CAAU,IAAI,CAAA,GAAI,MAAA;AAAA,QAC5C;AAAA,OACD,CAAA;AAED,MAAA,IAAI,CAAC,IAAI,EAAA,EAAI;AACX,QAAA,MAAM,IAAI,SAAA,CAAU,GAAA,CAAI,MAAA,EAAQ,IAAI,UAAU,CAAA;AAAA,MAChD;AACA,MAAA,IAAI,GAAA,CAAI,WAAW,GAAA,IAAO,GAAA,CAAI,QAAQ,GAAA,CAAI,gBAAgB,MAAM,GAAA,EAAK;AACnE,QAAA,OAAO,IAAA;AAAA,MACT;AACA,MAAA,OAAO,IAAI,IAAA,EAAK;AAAA,IAClB,CAAA;AAAA,IACA,OAAA,EAAS;AAAA,GACX;AACF;AAiBO,IAAM,SAAA,GAAN,cAAwB,KAAA,CAAM;AAAA,EACnC,WAAA,CAEkB,QAEA,UAAA,EAChB;AACA,IAAA,KAAA,CAAM,CAAA,KAAA,EAAQ,MAAM,CAAA,EAAA,EAAK,UAAU,CAAA,CAAE,CAAA;AAJrB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAEA,IAAA,IAAA,CAAA,UAAA,GAAA,UAAA;AAGhB,IAAA,IAAA,CAAK,IAAA,GAAO,WAAA;AAAA,EACd;AACF","file":"index.cjs","sourcesContent":["import type { Executor, ExecutorMiddleware } from \"@routar/core\";\nimport { createExecutor, serializeParams } from \"@routar/core\";\n\n/**\n * Creates an {@link Executor} backed by the browser / Node.js `fetch` API.\n *\n * Suited for SSR environments where you need per-request dynamic headers\n * (e.g. forwarding auth cookies) without sharing state across requests.\n *\n * - Query params are serialized and appended to the URL.\n * - A `Content-Type: application/json` header is added automatically when\n * a request body is present.\n * - Responses with status 204 or `Content-Length: 0` resolve to `null`.\n * - Non-2xx responses throw an {@link HttpError}.\n *\n * @param baseURL - Absolute base URL prepended to every endpoint path.\n * @param options.defaultHeaders - Async factory called on every request to\n * produce headers (e.g. reading cookies in a Next.js server component).\n * @param options.middlewares - Middleware chain applied before the fetch call.\n *\n * @example\n * ```ts\n * const executor = createFetchExecutor('https://api.example.com', {\n * defaultHeaders: async () => {\n * const token = await getServerToken();\n * return token ? { Authorization: `Bearer ${token}` } : {};\n * },\n * });\n * ```\n */\nexport function createFetchExecutor(\n baseURL: string,\n options?: {\n defaultHeaders?: () =>\n | Record<string, string>\n | Promise<Record<string, string>>;\n middlewares?: ExecutorMiddleware[];\n },\n): Executor {\n return createExecutor(\n async ({ method, url, params, body, headers, signal }) => {\n const fullURL = new URL(baseURL.replace(/\\/$/, '') + url);\n if (params) {\n serializeParams(params).forEach((v, k) =>\n fullURL.searchParams.set(k, v),\n );\n }\n\n const defaultHeaders = (await options?.defaultHeaders?.()) ?? {};\n\n const res = await fetch(fullURL.toString(), {\n method,\n headers: {\n ...(body != null ? { \"Content-Type\": \"application/json\" } : {}),\n ...defaultHeaders,\n ...headers,\n },\n body: body != null ? JSON.stringify(body) : undefined,\n signal,\n });\n\n if (!res.ok) {\n throw new HttpError(res.status, res.statusText);\n }\n if (res.status === 204 || res.headers.get(\"content-length\") === \"0\") {\n return null;\n }\n return res.json();\n },\n options?.middlewares,\n );\n}\n\n/**\n * Thrown by {@link createFetchExecutor} when the server returns a non-2xx\n * status code.\n *\n * @example\n * ```ts\n * try {\n * await api.getDetail({ path: { id: 999 } });\n * } catch (err) {\n * if (err instanceof HttpError && err.status === 404) {\n * // handle not-found\n * }\n * }\n * ```\n */\nexport class HttpError extends Error {\n constructor(\n /** HTTP status code (e.g. 404, 500). */\n public readonly status: number,\n /** HTTP status text (e.g. \"Not Found\"). */\n public readonly statusText: string,\n ) {\n super(`HTTP ${status}: ${statusText}`);\n this.name = \"HttpError\";\n }\n}\n"]}
1
+ {"version":3,"sources":["../src/create-fetch-executor.ts"],"names":["createExecutor","serializeParams"],"mappings":";;;;;AA8B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,IAClB,CAAA;AAAA,IACA,OAAA,EAAS;AAAA,GACX;AACF;AAiBO,IAAM,SAAA,GAAN,cAAwB,KAAA,CAAM;AAAA,EACnC,WAAA,CAEkB,QAEA,UAAA,EAChB;AACA,IAAA,KAAA,CAAM,CAAA,KAAA,EAAQ,MAAM,CAAA,EAAA,EAAK,UAAU,CAAA,CAAE,CAAA;AAJrB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAEA,IAAA,IAAA,CAAA,UAAA,GAAA,UAAA;AAGhB,IAAA,IAAA,CAAK,IAAA,GAAO,WAAA;AAAA,EACd;AACF","file":"index.cjs","sourcesContent":["import type { Executor, ExecutorMiddleware } from \"@routar/core\";\nimport { createExecutor, serializeParams } from \"@routar/core\";\n\n/**\n * Creates an {@link Executor} backed by the browser / Node.js `fetch` API.\n *\n * Suited for SSR environments where you need per-request dynamic headers\n * (e.g. forwarding auth cookies) without sharing state across requests.\n *\n * - Query params are serialized and appended to the URL.\n * - A `Content-Type: application/json` header is added automatically when\n * a request body is present.\n * - Responses with status 204 or `Content-Length: 0` resolve to `null`.\n * - Non-2xx responses throw an {@link HttpError}.\n *\n * @param baseURL - Absolute base URL prepended to every endpoint path.\n * @param options.defaultHeaders - Async factory called on every request to\n * produce headers (e.g. reading cookies in a Next.js server component).\n * @param options.middlewares - Middleware chain applied before the fetch call.\n *\n * @example\n * ```ts\n * const executor = createFetchExecutor('https://api.example.com', {\n * defaultHeaders: async () => {\n * const token = await getServerToken();\n * return token ? { Authorization: `Bearer ${token}` } : {};\n * },\n * });\n * ```\n */\nexport function createFetchExecutor(\n baseURL: string,\n options?: {\n defaultHeaders?: () =>\n | Record<string, string>\n | Promise<Record<string, string>>;\n middlewares?: ExecutorMiddleware[];\n },\n): Executor {\n return createExecutor(\n async ({ method, url, params, body, headers, signal }) => {\n const fullURL = new URL(baseURL.replace(/\\/$/, \"\") + url);\n if (params) {\n serializeParams(params).forEach((v, k) => {\n fullURL.searchParams.set(k, v);\n });\n }\n\n const defaultHeaders = (await options?.defaultHeaders?.()) ?? {};\n\n const res = await fetch(fullURL.toString(), {\n method,\n headers: {\n ...(body != null ? { \"Content-Type\": \"application/json\" } : {}),\n ...defaultHeaders,\n ...headers,\n },\n body: body != null ? JSON.stringify(body) : undefined,\n signal,\n });\n\n if (!res.ok) {\n throw new HttpError(res.status, res.statusText);\n }\n if (res.status === 204 || res.headers.get(\"content-length\") === \"0\") {\n return null;\n }\n return res.json();\n },\n options?.middlewares,\n );\n}\n\n/**\n * Thrown by {@link createFetchExecutor} when the server returns a non-2xx\n * status code.\n *\n * @example\n * ```ts\n * try {\n * await api.getDetail({ path: { id: 999 } });\n * } catch (err) {\n * if (err instanceof HttpError && err.status === 404) {\n * // handle not-found\n * }\n * }\n * ```\n */\nexport class HttpError extends Error {\n constructor(\n /** HTTP status code (e.g. 404, 500). */\n public readonly status: number,\n /** HTTP status text (e.g. \"Not Found\"). */\n public readonly statusText: string,\n ) {\n super(`HTTP ${status}: ${statusText}`);\n this.name = \"HttpError\";\n }\n}\n"]}
package/dist/index.js CHANGED
@@ -6,9 +6,9 @@ function createFetchExecutor(baseURL, options) {
6
6
  async ({ method, url, params, body, headers, signal }) => {
7
7
  const fullURL = new URL(baseURL.replace(/\/$/, "") + url);
8
8
  if (params) {
9
- serializeParams(params).forEach(
10
- (v, k) => fullURL.searchParams.set(k, v)
11
- );
9
+ serializeParams(params).forEach((v, k) => {
10
+ fullURL.searchParams.set(k, v);
11
+ });
12
12
  }
13
13
  const defaultHeaders = await options?.defaultHeaders?.() ?? {};
14
14
  const res = await fetch(fullURL.toString(), {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/create-fetch-executor.ts"],"names":[],"mappings":";;;AA8BO,SAAS,mBAAA,CACd,SACA,OAAA,EAMU;AACV,EAAA,OAAO,cAAA;AAAA,IACL,OAAO,EAAE,MAAA,EAAQ,GAAA,EAAK,QAAQ,IAAA,EAAM,OAAA,EAAS,QAAO,KAAM;AACxD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A,EAAA,EAAK,UAAU,CAAA,CAAE,CAAA;AAJrB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAEA,IAAA,IAAA,CAAA,UAAA,GAAA,UAAA;AAGhB,IAAA,IAAA,CAAK,IAAA,GAAO,WAAA;AAAA,EACd;AACF","file":"index.js","sourcesContent":["import type { Executor, ExecutorMiddleware } from \"@routar/core\";\nimport { createExecutor, serializeParams } from \"@routar/core\";\n\n/**\n * Creates an {@link Executor} backed by the browser / Node.js `fetch` API.\n *\n * Suited for SSR environments where you need per-request dynamic headers\n * (e.g. forwarding auth cookies) without sharing state across requests.\n *\n * - Query params are serialized and appended to the URL.\n * - A `Content-Type: application/json` header is added automatically when\n * a request body is present.\n * - Responses with status 204 or `Content-Length: 0` resolve to `null`.\n * - Non-2xx responses throw an {@link HttpError}.\n *\n * @param baseURL - Absolute base URL prepended to every endpoint path.\n * @param options.defaultHeaders - Async factory called on every request to\n * produce headers (e.g. reading cookies in a Next.js server component).\n * @param options.middlewares - Middleware chain applied before the fetch call.\n *\n * @example\n * ```ts\n * const executor = createFetchExecutor('https://api.example.com', {\n * defaultHeaders: async () => {\n * const token = await getServerToken();\n * return token ? { Authorization: `Bearer ${token}` } : {};\n * },\n * });\n * ```\n */\nexport function createFetchExecutor(\n baseURL: string,\n options?: {\n defaultHeaders?: () =>\n | Record<string, string>\n | Promise<Record<string, string>>;\n middlewares?: ExecutorMiddleware[];\n },\n): Executor {\n return createExecutor(\n async ({ method, url, params, body, headers, signal }) => {\n const fullURL = new URL(baseURL.replace(/\\/$/, '') + url);\n if (params) {\n serializeParams(params).forEach((v, k) =>\n fullURL.searchParams.set(k, v),\n );\n }\n\n const defaultHeaders = (await options?.defaultHeaders?.()) ?? {};\n\n const res = await fetch(fullURL.toString(), {\n method,\n headers: {\n ...(body != null ? { \"Content-Type\": \"application/json\" } : {}),\n ...defaultHeaders,\n ...headers,\n },\n body: body != null ? JSON.stringify(body) : undefined,\n signal,\n });\n\n if (!res.ok) {\n throw new HttpError(res.status, res.statusText);\n }\n if (res.status === 204 || res.headers.get(\"content-length\") === \"0\") {\n return null;\n }\n return res.json();\n },\n options?.middlewares,\n );\n}\n\n/**\n * Thrown by {@link createFetchExecutor} when the server returns a non-2xx\n * status code.\n *\n * @example\n * ```ts\n * try {\n * await api.getDetail({ path: { id: 999 } });\n * } catch (err) {\n * if (err instanceof HttpError && err.status === 404) {\n * // handle not-found\n * }\n * }\n * ```\n */\nexport class HttpError extends Error {\n constructor(\n /** HTTP status code (e.g. 404, 500). */\n public readonly status: number,\n /** HTTP status text (e.g. \"Not Found\"). */\n public readonly statusText: string,\n ) {\n super(`HTTP ${status}: ${statusText}`);\n this.name = \"HttpError\";\n }\n}\n"]}
1
+ {"version":3,"sources":["../src/create-fetch-executor.ts"],"names":[],"mappings":";;;AA8BO,SAAS,mBAAA,CACd,SACA,OAAA,EAMU;AACV,EAAA,OAAO,cAAA;AAAA,IACL,OAAO,EAAE,MAAA,EAAQ,GAAA,EAAK,QAAQ,IAAA,EAAM,OAAA,EAAS,QAAO,KAAM;AACxD,MAAA,MAAM,OAAA,GAAU,IAAI,GAAA,CAAI,OAAA,CAAQ,QAAQ,KAAA,EAAO,EAAE,IAAI,GAAG,CAAA;AACxD,MAAA,IAAI,MAAA,EAAQ;AACV,QAAA,eAAA,CAAgB,MAAM,CAAA,CAAE,OAAA,CAAQ,CAAC,GAAG,CAAA,KAAM;AACxC,UAAA,OAAA,CAAQ,YAAA,CAAa,GAAA,CAAI,CAAA,EAAG,CAAC,CAAA;AAAA,QAC/B,CAAC,CAAA;AAAA,MACH;AAEA,MAAA,MAAM,cAAA,GAAkB,MAAM,OAAA,EAAS,cAAA,QAAuB,EAAC;AAE/D,MAAA,MAAM,GAAA,GAAM,MAAM,KAAA,CAAM,OAAA,CAAQ,UAAS,EAAG;AAAA,QAC1C,MAAA;AAAA,QACA,OAAA,EAAS;AAAA,UACP,GAAI,IAAA,IAAQ,IAAA,GAAO,EAAE,cAAA,EAAgB,kBAAA,KAAuB,EAAC;AAAA,UAC7D,GAAG,cAAA;AAAA,UACH,GAAG;AAAA,SACL;AAAA,QACA,MAAM,IAAA,IAAQ,IAAA,GAAO,IAAA,CAAK,SAAA,CAAU,IAAI,CAAA,GAAI,MAAA;AAAA,QAC5C;AAAA,OACD,CAAA;AAED,MAAA,IAAI,CAAC,IAAI,EAAA,EAAI;AACX,QAAA,MAAM,IAAI,SAAA,CAAU,GAAA,CAAI,MAAA,EAAQ,IAAI,UAAU,CAAA;AAAA,MAChD;AACA,MAAA,IAAI,GAAA,CAAI,WAAW,GAAA,IAAO,GAAA,CAAI,QAAQ,GAAA,CAAI,gBAAgB,MAAM,GAAA,EAAK;AACnE,QAAA,OAAO,IAAA;AAAA,MACT;AACA,MAAA,OAAO,IAAI,IAAA,EAAK;AAAA,IAClB,CAAA;AAAA,IACA,OAAA,EAAS;AAAA,GACX;AACF;AAiBO,IAAM,SAAA,GAAN,cAAwB,KAAA,CAAM;AAAA,EACnC,WAAA,CAEkB,QAEA,UAAA,EAChB;AACA,IAAA,KAAA,CAAM,CAAA,KAAA,EAAQ,MAAM,CAAA,EAAA,EAAK,UAAU,CAAA,CAAE,CAAA;AAJrB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAEA,IAAA,IAAA,CAAA,UAAA,GAAA,UAAA;AAGhB,IAAA,IAAA,CAAK,IAAA,GAAO,WAAA;AAAA,EACd;AACF","file":"index.js","sourcesContent":["import type { Executor, ExecutorMiddleware } from \"@routar/core\";\nimport { createExecutor, serializeParams } from \"@routar/core\";\n\n/**\n * Creates an {@link Executor} backed by the browser / Node.js `fetch` API.\n *\n * Suited for SSR environments where you need per-request dynamic headers\n * (e.g. forwarding auth cookies) without sharing state across requests.\n *\n * - Query params are serialized and appended to the URL.\n * - A `Content-Type: application/json` header is added automatically when\n * a request body is present.\n * - Responses with status 204 or `Content-Length: 0` resolve to `null`.\n * - Non-2xx responses throw an {@link HttpError}.\n *\n * @param baseURL - Absolute base URL prepended to every endpoint path.\n * @param options.defaultHeaders - Async factory called on every request to\n * produce headers (e.g. reading cookies in a Next.js server component).\n * @param options.middlewares - Middleware chain applied before the fetch call.\n *\n * @example\n * ```ts\n * const executor = createFetchExecutor('https://api.example.com', {\n * defaultHeaders: async () => {\n * const token = await getServerToken();\n * return token ? { Authorization: `Bearer ${token}` } : {};\n * },\n * });\n * ```\n */\nexport function createFetchExecutor(\n baseURL: string,\n options?: {\n defaultHeaders?: () =>\n | Record<string, string>\n | Promise<Record<string, string>>;\n middlewares?: ExecutorMiddleware[];\n },\n): Executor {\n return createExecutor(\n async ({ method, url, params, body, headers, signal }) => {\n const fullURL = new URL(baseURL.replace(/\\/$/, \"\") + url);\n if (params) {\n serializeParams(params).forEach((v, k) => {\n fullURL.searchParams.set(k, v);\n });\n }\n\n const defaultHeaders = (await options?.defaultHeaders?.()) ?? {};\n\n const res = await fetch(fullURL.toString(), {\n method,\n headers: {\n ...(body != null ? { \"Content-Type\": \"application/json\" } : {}),\n ...defaultHeaders,\n ...headers,\n },\n body: body != null ? JSON.stringify(body) : undefined,\n signal,\n });\n\n if (!res.ok) {\n throw new HttpError(res.status, res.statusText);\n }\n if (res.status === 204 || res.headers.get(\"content-length\") === \"0\") {\n return null;\n }\n return res.json();\n },\n options?.middlewares,\n );\n}\n\n/**\n * Thrown by {@link createFetchExecutor} when the server returns a non-2xx\n * status code.\n *\n * @example\n * ```ts\n * try {\n * await api.getDetail({ path: { id: 999 } });\n * } catch (err) {\n * if (err instanceof HttpError && err.status === 404) {\n * // handle not-found\n * }\n * }\n * ```\n */\nexport class HttpError extends Error {\n constructor(\n /** HTTP status code (e.g. 404, 500). */\n public readonly status: number,\n /** HTTP status text (e.g. \"Not Found\"). */\n public readonly statusText: string,\n ) {\n super(`HTTP ${status}: ${statusText}`);\n this.name = \"HttpError\";\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routar/fetch",
3
- "version": "0.1.1",
3
+ "version": "1.0.0",
4
4
  "description": "Fetch-based executor for routar — transport adapter using the native fetch API",
5
5
  "keywords": [
6
6
  "api",
@@ -39,7 +39,7 @@
39
39
  "dev": "tsup --watch"
40
40
  },
41
41
  "peerDependencies": {
42
- "@routar/core": "^0.1.0"
42
+ "@routar/core": "^1.0.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@routar/core": "workspace:*",