posipaki 0.7.1 → 0.8.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/xfetch.d.ts CHANGED
@@ -12,10 +12,12 @@ type FetchArgs<T> = {
12
12
  url: URL;
13
13
  method?: "GET";
14
14
  body?: undefined;
15
+ headers?: Record<string, string>;
15
16
  } | {
16
17
  url: URL;
17
18
  method?: "POST" | "PUT" | "PATCH";
18
19
  body: T;
20
+ headers?: Record<string, string>;
19
21
  };
20
22
  /** Messages emitted by a fetch process during its lifecycle. */
21
23
  type FetchMessage<T> = {
@@ -36,10 +38,11 @@ declare function xfetch<Type>({
36
38
  pname,
37
39
  toParent,
38
40
  send
39
- }: ProcessCtx<FetchMessage<Type>, FetchMessage<Type>>, {
41
+ }: ProcessCtx<FetchArgs<Type>, FetchState<Type>, FetchMessage<Type>, FetchMessage<Type>>, {
40
42
  method,
41
43
  url,
42
- body
44
+ body,
45
+ headers: callerHeaders
43
46
  }: FetchArgs<Type>): Generator<FetchState<Type> | null, void, FetchMessage<Type>>;
44
47
  //#endregion
45
48
  export { FetchArgs, FetchMessage, FetchProcess, FetchState, xfetch };
@@ -1 +1 @@
1
- {"version":3,"file":"xfetch.d.ts","names":[],"sources":["../src/xfetch.ts"],"mappings":";;;;KAOY,UAAA;EACV,IAAA;EACA,IAAA,EAAM,CAAC;EACP,IAAA;AAAA;;KAIU,SAAA;EACN,GAAA,EAAK,GAAA;EAAK,MAAA;EAAgB,IAAA;AAAA;EAC1B,GAAA,EAAK,GAAA;EAAK,MAAA;EAAmC,IAAA,EAAM,CAAA;AAAA;;KAG7C,YAAA;EACN,IAAA;EAAY,IAAA,GAAO,CAAA;EAAU,IAAA;AAAA;EAC7B,IAAA;AAAA,IACF,WAAW;;KAGH,YAAA,MAAkB,OAAA,CAC5B,SAAA,CAAU,CAAA,GACV,UAAA,CAAW,CAAA,GACX,YAAA,CAAa,CAAA,GACb,YAAA,CAAa,CAAA;;;;;;iBAiBL,MAAA;EACN,KAAA;EAAO,QAAA;EAAU;AAAA,GAAQ,UAAA,CAAW,YAAA,CAAa,IAAA,GAAO,YAAA,CAAa,IAAA;EACrE,MAAA;EAAgB,GAAA;EAAK;AAAA,GAAQ,SAAA,CAAU,IAAA,IACxC,SAAA,CAAU,UAAA,CAAW,IAAA,gBAAoB,YAAA,CAAa,IAAA"}
1
+ {"version":3,"file":"xfetch.d.ts","names":[],"sources":["../src/xfetch.ts"],"mappings":";;;;KAOY,UAAA;EACV,IAAA;EACA,IAAA,EAAM,CAAC;EACP,IAAA;AAAA;;KAIU,SAAA;EAEN,GAAA,EAAK,GAAA;EACL,MAAA;EACA,IAAA;EACA,OAAA,GAAU,MAAA;AAAA;EAGV,GAAA,EAAK,GAAA;EACL,MAAA;EACA,IAAA,EAAM,CAAA;EACN,OAAA,GAAU,MAAA;AAAA;;KAIJ,YAAA;EACN,IAAA;EAAY,IAAA,GAAO,CAAA;EAAU,IAAA;AAAA;EAC7B,IAAA;AAAA,IACF,WAAW;;KAGH,YAAA,MAAkB,OAAA,CAC5B,SAAA,CAAU,CAAA,GACV,UAAA,CAAW,CAAA,GACX,YAAA,CAAa,CAAA,GACb,YAAA,CAAa,CAAA;;;;;;iBAiBL,MAAA;EAEN,KAAA;EACA,QAAA;EACA;AAAA,GACC,UAAA,CACD,SAAA,CAAU,IAAA,GACV,UAAA,CAAW,IAAA,GACX,YAAA,CAAa,IAAA,GACb,YAAA,CAAa,IAAA;EAEb,MAAA;EAAgB,GAAA;EAAK,IAAA;EAAM,OAAA,EAAS;AAAA,GAAiB,SAAA,CAAU,IAAA,IAChE,SAAA,CAAU,UAAA,CAAW,IAAA,gBAAoB,YAAA,CAAa,IAAA"}
package/dist/xfetch.js CHANGED
@@ -8,7 +8,7 @@ function isJsonHelper(res) {
8
8
  * JSON detection, abort via AbortController, and yields a
9
9
  * {@link FetchState} with the current status.
10
10
  */
11
- function* xfetch({ pname, toParent, send }, { method = "GET", url, body }) {
11
+ function* xfetch({ pname, toParent, send }, { method = "GET", url, body, headers: callerHeaders }) {
12
12
  const state = {
13
13
  code: "pending",
14
14
  data: null,
@@ -22,7 +22,7 @@ function* xfetch({ pname, toParent, send }, { method = "GET", url, body }) {
22
22
  try {
23
23
  toSelf({ type: "LOADING" });
24
24
  const serializedBody = method === "GET" ? void 0 : JSON.stringify(body);
25
- const headers = new Headers({});
25
+ const headers = new Headers(callerHeaders ?? {});
26
26
  if (serializedBody) headers.set("content-type", "application/json");
27
27
  const res = await fetch(url.href, {
28
28
  method,
@@ -61,7 +61,7 @@ function* xfetch({ pname, toParent, send }, { method = "GET", url, body }) {
61
61
  state.data = msg.data || null;
62
62
  state.text = msg.text || null;
63
63
  }
64
- }, isDone, true);
64
+ }, isDone, false);
65
65
  }
66
66
  //#endregion
67
67
  export { xfetch };
@@ -1 +1 @@
1
- {"version":3,"file":"xfetch.js","names":[],"sources":["../src/xfetch.ts"],"sourcesContent":["import { runDispatch } from \"./util.js\";\nimport type { ExitMessage, ProcessCtx } from \"./types.js\";\nimport type { Process } from \"./process.js\";\n\n// ---- types ------------------------------------------------------------------\n\n/** State of an in-flight or completed fetch. */\nexport type FetchState<T> = {\n code: \"pending\" | \"loading\" | \"aborted\" | \"failed\" | \"ok\";\n data: T | null;\n text: string | null;\n};\n\n/** Arguments for a fetch process. */\nexport type FetchArgs<T> =\n | { url: URL; method?: \"GET\"; body?: undefined }\n | { url: URL; method?: \"POST\" | \"PUT\" | \"PATCH\"; body: T };\n\n/** Messages emitted by a fetch process during its lifecycle. */\nexport type FetchMessage<T> =\n | { type: \"OK\"; data?: T | null; text?: string | null }\n | { type: \"ERROR\" | \"LOADING\" | \"ABORTED\" | \"STOP\" }\n | ExitMessage;\n\n/** A process that performs an HTTP fetch. */\nexport type FetchProcess<D> = Process<\n FetchArgs<D>,\n FetchState<D>,\n FetchMessage<D>,\n FetchMessage<D>\n>;\n\n// ---- helpers ----------------------------------------------------------------\n\nfunction isJsonHelper(res: Response): boolean {\n const ct = res.headers.get(\"content-type\");\n return ct === \"application/json\";\n}\n\n// ---- xfetch -----------------------------------------------------------------\n\n/**\n * A fetch wrapper implemented as a process. Supports GET/POST/PUT/PATCH,\n * JSON detection, abort via AbortController, and yields a\n * {@link FetchState} with the current status.\n */\nfunction* xfetch<Type>(\n { pname, toParent, send }: ProcessCtx<FetchMessage<Type>, FetchMessage<Type>>,\n { method = \"GET\", url, body }: FetchArgs<Type>,\n): Generator<FetchState<Type> | null, void, FetchMessage<Type>> {\n const state: FetchState<Type> = { code: \"pending\", data: null, text: null };\n yield state;\n\n const controller = new AbortController();\n const signal = controller.signal;\n const toSelf = send;\n\n (async function doRequest() {\n try {\n toSelf({ type: \"LOADING\" });\n const serializedBody =\n method === \"GET\" ? undefined : JSON.stringify(body);\n const headers = new Headers({});\n if (serializedBody) {\n headers.set(\"content-type\", \"application/json\");\n }\n const res: Response = await fetch(url.href, {\n method,\n signal,\n body: serializedBody,\n headers,\n });\n if (isJsonHelper(res)) {\n const data = await res.json();\n toSelf({ type: \"OK\", data });\n } else {\n const text = await res.text();\n toSelf({ type: \"OK\", text });\n }\n } catch (e) {\n const isAborted = e instanceof DOMException && e.name === \"AbortError\";\n if (isAborted) {\n toSelf({ type: \"ABORTED\" });\n } else {\n toSelf({ type: \"ERROR\" });\n }\n }\n })();\n\n const isDone = (): boolean =>\n !(state.code === \"pending\" || state.code === \"loading\");\n\n yield* runDispatch<FetchMessage<Type>>(\n pname,\n (msg: FetchMessage<Type>) => {\n if (msg.type === \"STOP\") {\n controller.abort();\n }\n if (msg.type === \"ABORTED\") {\n toParent(msg);\n state.code = \"aborted\";\n }\n if (msg.type === \"ERROR\") {\n toParent(msg);\n state.code = \"failed\";\n }\n if (msg.type === \"LOADING\") {\n state.code = \"loading\";\n }\n if (msg.type === \"OK\") {\n toParent(msg);\n state.code = \"ok\";\n state.data = msg.data || null;\n state.text = msg.text || null;\n }\n },\n isDone,\n true,\n );\n}\n\nexport { xfetch };\n"],"mappings":";;A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AAI,IAAI,SAAS,MAAM;GACrB,SAAS,GAAG;GACZ,MAAM,OAAO;GACb,MAAM,OAAO,IAAI,QAAQ;GACzB,MAAM,OAAO,IAAI,QAAQ;EAC3B;CACF,GACA,QACA,IACF;AACF"}
1
+ {"version":3,"file":"xfetch.js","names":[],"sources":["../src/xfetch.ts"],"sourcesContent":["import { runDispatch } from \"./util.js\";\nimport type { ExitMessage, ProcessCtx } from \"./types.js\";\nimport type { Process } from \"./process.js\";\n\n// ---- types ------------------------------------------------------------------\n\n/** State of an in-flight or completed fetch. */\nexport type FetchState<T> = {\n code: \"pending\" | \"loading\" | \"aborted\" | \"failed\" | \"ok\";\n data: T | null;\n text: string | null;\n};\n\n/** Arguments for a fetch process. */\nexport type FetchArgs<T> =\n | {\n url: URL;\n method?: \"GET\";\n body?: undefined;\n headers?: Record<string, string>;\n }\n | {\n url: URL;\n method?: \"POST\" | \"PUT\" | \"PATCH\";\n body: T;\n headers?: Record<string, string>;\n };\n\n/** Messages emitted by a fetch process during its lifecycle. */\nexport type FetchMessage<T> =\n | { type: \"OK\"; data?: T | null; text?: string | null }\n | { type: \"ERROR\" | \"LOADING\" | \"ABORTED\" | \"STOP\" }\n | ExitMessage;\n\n/** A process that performs an HTTP fetch. */\nexport type FetchProcess<D> = Process<\n FetchArgs<D>,\n FetchState<D>,\n FetchMessage<D>,\n FetchMessage<D>\n>;\n\n// ---- helpers ----------------------------------------------------------------\n\nfunction isJsonHelper(res: Response): boolean {\n const ct = res.headers.get(\"content-type\");\n return ct === \"application/json\";\n}\n\n// ---- xfetch -----------------------------------------------------------------\n\n/**\n * A fetch wrapper implemented as a process. Supports GET/POST/PUT/PATCH,\n * JSON detection, abort via AbortController, and yields a\n * {@link FetchState} with the current status.\n */\nfunction* xfetch<Type>(\n {\n pname,\n toParent,\n send,\n }: ProcessCtx<\n FetchArgs<Type>,\n FetchState<Type>,\n FetchMessage<Type>,\n FetchMessage<Type>\n >,\n { method = \"GET\", url, body, headers: callerHeaders }: FetchArgs<Type>,\n): Generator<FetchState<Type> | null, void, FetchMessage<Type>> {\n const state: FetchState<Type> = { code: \"pending\", data: null, text: null };\n yield state;\n\n const controller = new AbortController();\n const signal = controller.signal;\n const toSelf = send;\n\n (async function doRequest() {\n try {\n toSelf({ type: \"LOADING\" });\n const serializedBody =\n method === \"GET\" ? undefined : JSON.stringify(body);\n const headers = new Headers(callerHeaders ?? {});\n if (serializedBody) {\n headers.set(\"content-type\", \"application/json\");\n }\n const res: Response = await fetch(url.href, {\n method,\n signal,\n body: serializedBody,\n headers,\n });\n if (isJsonHelper(res)) {\n const data = await res.json();\n toSelf({ type: \"OK\", data });\n } else {\n const text = await res.text();\n toSelf({ type: \"OK\", text });\n }\n } catch (e) {\n const isAborted = e instanceof DOMException && e.name === \"AbortError\";\n if (isAborted) {\n toSelf({ type: \"ABORTED\" });\n } else {\n toSelf({ type: \"ERROR\" });\n }\n }\n })();\n\n const isDone = (): boolean =>\n !(state.code === \"pending\" || state.code === \"loading\");\n\n yield* runDispatch<FetchMessage<Type>>(\n pname,\n (msg: FetchMessage<Type>) => {\n if (msg.type === \"STOP\") {\n controller.abort();\n }\n if (msg.type === \"ABORTED\") {\n toParent(msg);\n state.code = \"aborted\";\n }\n if (msg.type === \"ERROR\") {\n toParent(msg);\n state.code = \"failed\";\n }\n if (msg.type === \"LOADING\") {\n state.code = \"loading\";\n }\n if (msg.type === \"OK\") {\n toParent(msg);\n state.code = \"ok\";\n state.data = msg.data || null;\n state.text = msg.text || null;\n }\n },\n isDone,\n false,\n );\n}\n\nexport { xfetch };\n"],"mappings":";;AA4CA,SAAS,aAAa,KAAwB;CAE5C,OADW,IAAI,QAAQ,IAAI,cACnB,MAAM;AAChB;;;;;;AASA,UAAU,OACR,EACE,OACA,UACA,QAOF,EAAE,SAAS,OAAO,KAAK,MAAM,SAAS,iBACwB;CAC9D,MAAM,QAA0B;EAAE,MAAM;EAAW,MAAM;EAAM,MAAM;CAAK;CAC1E,MAAM;CAEN,MAAM,a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"version": "0.7.1",
4
+ "version": "0.8.0",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "files": [