@puckeditor/plugin-pages 0.9.0-canary.f8c1abd5 → 0.9.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.d.mts CHANGED
@@ -2,6 +2,13 @@ import * as _puckeditor_core_dist_actions_Dgllwd_W from '@puckeditor/core/dist/a
2
2
  import * as react from 'react';
3
3
  import * as react_jsx_runtime from 'react/jsx-runtime';
4
4
 
5
+ /** A request to the pages API, as `prepareRequest` receives and returns it. */
6
+ type RequestOptions = {
7
+ /** The JSON body. Only POST requests send one. */
8
+ body?: Record<string, any>;
9
+ headers?: HeadersInit;
10
+ credentials?: RequestCredentials;
11
+ };
5
12
  type PagePluginOptions = {
6
13
  /** Where `puckHandler` serves the pages API. Defaults to `/api/puck/pages`. */
7
14
  host?: string;
@@ -9,6 +16,11 @@ type PagePluginOptions = {
9
16
  siteUrl?: string;
10
17
  /** The page to open first. Defaults to the `path` search param. */
11
18
  initialPath?: string;
19
+ /**
20
+ * Customizes each request to the pages API, to add headers, add metadata to the body,
21
+ * or change credentials. The headers and body it returns are merged over the defaults.
22
+ */
23
+ prepareRequest?: (opts: RequestOptions) => RequestOptions | Promise<RequestOptions>;
12
24
  };
13
25
 
14
26
  declare function createPagesPlugin(opts?: PagePluginOptions): {
package/dist/index.d.ts CHANGED
@@ -2,6 +2,13 @@ import * as _puckeditor_core_dist_actions_Dgllwd_W from '@puckeditor/core/dist/a
2
2
  import * as react from 'react';
3
3
  import * as react_jsx_runtime from 'react/jsx-runtime';
4
4
 
5
+ /** A request to the pages API, as `prepareRequest` receives and returns it. */
6
+ type RequestOptions = {
7
+ /** The JSON body. Only POST requests send one. */
8
+ body?: Record<string, any>;
9
+ headers?: HeadersInit;
10
+ credentials?: RequestCredentials;
11
+ };
5
12
  type PagePluginOptions = {
6
13
  /** Where `puckHandler` serves the pages API. Defaults to `/api/puck/pages`. */
7
14
  host?: string;
@@ -9,6 +16,11 @@ type PagePluginOptions = {
9
16
  siteUrl?: string;
10
17
  /** The page to open first. Defaults to the `path` search param. */
11
18
  initialPath?: string;
19
+ /**
20
+ * Customizes each request to the pages API, to add headers, add metadata to the body,
21
+ * or change credentials. The headers and body it returns are merged over the defaults.
22
+ */
23
+ prepareRequest?: (opts: RequestOptions) => RequestOptions | Promise<RequestOptions>;
12
24
  };
13
25
 
14
26
  declare function createPagesPlugin(opts?: PagePluginOptions): {
package/dist/index.js CHANGED
@@ -8556,11 +8556,17 @@ var toVersionSummary = (page) => ({
8556
8556
  versionNumber: page.version,
8557
8557
  createdAt: page.createdAt
8558
8558
  });
8559
- var post = (url, body) => fetch(url, {
8560
- method: "POST",
8561
- headers: { "content-type": "application/json" },
8562
- body: JSON.stringify(body ?? {})
8563
- });
8559
+ var mergeRequest = (defaults, prepared) => {
8560
+ const headers = new Headers(defaults.headers);
8561
+ new Headers(prepared.headers).forEach(
8562
+ (value, key) => headers.set(key, value)
8563
+ );
8564
+ return {
8565
+ headers,
8566
+ credentials: prepared.credentials ?? defaults.credentials,
8567
+ body: { ...defaults.body, ...prepared.body }
8568
+ };
8569
+ };
8564
8570
  var PageApiError = class extends Error {
8565
8571
  constructor(status, body) {
8566
8572
  super(`Pages request failed with ${status}`);
@@ -8584,10 +8590,25 @@ var toQuery = (params) => {
8584
8590
  };
8585
8591
  var usePageApi = (opts) => {
8586
8592
  const host = opts?.host ?? "/api/puck/pages";
8593
+ const prepareRequest = opts?.prepareRequest;
8587
8594
  return (0, import_react24.useMemo)(() => {
8595
+ const send = async (url, method, body) => {
8596
+ const hasBody = method === "POST";
8597
+ const defaults = hasBody ? {
8598
+ headers: { "content-type": "application/json" },
8599
+ body: body ?? {}
8600
+ } : {};
8601
+ const request = prepareRequest ? mergeRequest(defaults, await prepareRequest(defaults)) : defaults;
8602
+ return fetch(url, {
8603
+ method,
8604
+ headers: request.headers,
8605
+ credentials: request.credentials,
8606
+ body: hasBody ? JSON.stringify(request.body) : void 0
8607
+ });
8608
+ };
8588
8609
  const listPages = async (offset4) => {
8589
8610
  const list = await json(
8590
- fetch(`${host}${toQuery({ offset: offset4 })}`, { method: "GET" })
8611
+ send(`${host}${toQuery({ offset: offset4 })}`, "GET")
8591
8612
  );
8592
8613
  return {
8593
8614
  ...list,
@@ -8611,41 +8632,43 @@ var usePageApi = (opts) => {
8611
8632
  },
8612
8633
  /** Reads a page at a version, its latest unless `versionId` is given. */
8613
8634
  readPage: (id, versionId) => json(
8614
- fetch(`${host}/${id}${toQuery({ version: versionId })}`, {
8615
- method: "GET"
8616
- })
8635
+ send(`${host}/${id}${toQuery({ version: versionId })}`, "GET")
8617
8636
  ).then(toPageRead),
8618
8637
  /** Reads the page at a path, at its latest version. */
8619
- readPageByPath: (path) => json(
8620
- fetch(`${host}/by-path${toQuery({ path })}`, { method: "GET" })
8621
- ).then(toPageRead),
8638
+ readPageByPath: (path) => json(send(`${host}/by-path${toQuery({ path })}`, "GET")).then(
8639
+ toPageRead
8640
+ ),
8622
8641
  /** Reads a page's version history from `offset`, the newest by default. */
8623
8642
  readVersions: async (id, offset4) => {
8624
8643
  const list = await json(
8625
- fetch(`${host}/${id}/versions${toQuery({ offset: offset4 })}`, {
8626
- method: "GET"
8627
- })
8644
+ send(`${host}/${id}/versions${toQuery({ offset: offset4 })}`, "GET")
8628
8645
  );
8629
8646
  return { ...list, results: list.results.map(toVersionSummary) };
8630
8647
  },
8631
- createPage: (body) => json(post(host, body)).then(toPageRead),
8648
+ createPage: (body) => json(send(host, "POST", body)).then(toPageRead),
8632
8649
  /** Saves a new version of a page, refused with a 409 if a newer one was saved since its base. */
8633
8650
  saveVersion: (id, { data, baseVersionId }) => json(
8634
- post(`${host}/${id}/versions`, {
8651
+ send(`${host}/${id}/versions`, "POST", {
8635
8652
  data,
8636
8653
  previousVersion: baseVersionId === void 0 ? void 0 : Number(baseVersionId)
8637
8654
  })
8638
8655
  ).then(toPageRead),
8639
8656
  /** Moves a page, without saving a version. */
8640
- movePage: (id, path) => json(post(`${host}/${id}`, { path })).then(toPage),
8657
+ movePage: (id, path) => json(send(`${host}/${id}`, "POST", { path })).then(
8658
+ toPage
8659
+ ),
8641
8660
  /** Publishes one of a page's versions. */
8642
8661
  publishPage: (id, versionId) => json(
8643
- post(`${host}/${id}/publish`, { version: Number(versionId) })
8662
+ send(`${host}/${id}/publish`, "POST", {
8663
+ version: Number(versionId)
8664
+ })
8644
8665
  ).then(toPage),
8645
- unpublishPage: (id) => json(post(`${host}/${id}/unpublish`)).then(toPage),
8646
- deletePage: (id) => json(fetch(`${host}/${id}`, { method: "DELETE" }))
8666
+ unpublishPage: (id) => json(send(`${host}/${id}/unpublish`, "POST")).then(
8667
+ toPage
8668
+ ),
8669
+ deletePage: (id) => json(send(`${host}/${id}`, "DELETE"))
8647
8670
  };
8648
- }, [host]);
8671
+ }, [host, prepareRequest]);
8649
8672
  };
8650
8673
 
8651
8674
  // src/hooks/usePageEditorState.ts
package/dist/index.mjs CHANGED
@@ -8551,11 +8551,17 @@ var toVersionSummary = (page) => ({
8551
8551
  versionNumber: page.version,
8552
8552
  createdAt: page.createdAt
8553
8553
  });
8554
- var post = (url, body) => fetch(url, {
8555
- method: "POST",
8556
- headers: { "content-type": "application/json" },
8557
- body: JSON.stringify(body ?? {})
8558
- });
8554
+ var mergeRequest = (defaults, prepared) => {
8555
+ const headers = new Headers(defaults.headers);
8556
+ new Headers(prepared.headers).forEach(
8557
+ (value, key) => headers.set(key, value)
8558
+ );
8559
+ return {
8560
+ headers,
8561
+ credentials: prepared.credentials ?? defaults.credentials,
8562
+ body: { ...defaults.body, ...prepared.body }
8563
+ };
8564
+ };
8559
8565
  var PageApiError = class extends Error {
8560
8566
  constructor(status, body) {
8561
8567
  super(`Pages request failed with ${status}`);
@@ -8579,10 +8585,25 @@ var toQuery = (params) => {
8579
8585
  };
8580
8586
  var usePageApi = (opts) => {
8581
8587
  const host = opts?.host ?? "/api/puck/pages";
8588
+ const prepareRequest = opts?.prepareRequest;
8582
8589
  return useMemo20(() => {
8590
+ const send = async (url, method, body) => {
8591
+ const hasBody = method === "POST";
8592
+ const defaults = hasBody ? {
8593
+ headers: { "content-type": "application/json" },
8594
+ body: body ?? {}
8595
+ } : {};
8596
+ const request = prepareRequest ? mergeRequest(defaults, await prepareRequest(defaults)) : defaults;
8597
+ return fetch(url, {
8598
+ method,
8599
+ headers: request.headers,
8600
+ credentials: request.credentials,
8601
+ body: hasBody ? JSON.stringify(request.body) : void 0
8602
+ });
8603
+ };
8583
8604
  const listPages = async (offset4) => {
8584
8605
  const list = await json(
8585
- fetch(`${host}${toQuery({ offset: offset4 })}`, { method: "GET" })
8606
+ send(`${host}${toQuery({ offset: offset4 })}`, "GET")
8586
8607
  );
8587
8608
  return {
8588
8609
  ...list,
@@ -8606,41 +8627,43 @@ var usePageApi = (opts) => {
8606
8627
  },
8607
8628
  /** Reads a page at a version, its latest unless `versionId` is given. */
8608
8629
  readPage: (id, versionId) => json(
8609
- fetch(`${host}/${id}${toQuery({ version: versionId })}`, {
8610
- method: "GET"
8611
- })
8630
+ send(`${host}/${id}${toQuery({ version: versionId })}`, "GET")
8612
8631
  ).then(toPageRead),
8613
8632
  /** Reads the page at a path, at its latest version. */
8614
- readPageByPath: (path) => json(
8615
- fetch(`${host}/by-path${toQuery({ path })}`, { method: "GET" })
8616
- ).then(toPageRead),
8633
+ readPageByPath: (path) => json(send(`${host}/by-path${toQuery({ path })}`, "GET")).then(
8634
+ toPageRead
8635
+ ),
8617
8636
  /** Reads a page's version history from `offset`, the newest by default. */
8618
8637
  readVersions: async (id, offset4) => {
8619
8638
  const list = await json(
8620
- fetch(`${host}/${id}/versions${toQuery({ offset: offset4 })}`, {
8621
- method: "GET"
8622
- })
8639
+ send(`${host}/${id}/versions${toQuery({ offset: offset4 })}`, "GET")
8623
8640
  );
8624
8641
  return { ...list, results: list.results.map(toVersionSummary) };
8625
8642
  },
8626
- createPage: (body) => json(post(host, body)).then(toPageRead),
8643
+ createPage: (body) => json(send(host, "POST", body)).then(toPageRead),
8627
8644
  /** Saves a new version of a page, refused with a 409 if a newer one was saved since its base. */
8628
8645
  saveVersion: (id, { data, baseVersionId }) => json(
8629
- post(`${host}/${id}/versions`, {
8646
+ send(`${host}/${id}/versions`, "POST", {
8630
8647
  data,
8631
8648
  previousVersion: baseVersionId === void 0 ? void 0 : Number(baseVersionId)
8632
8649
  })
8633
8650
  ).then(toPageRead),
8634
8651
  /** Moves a page, without saving a version. */
8635
- movePage: (id, path) => json(post(`${host}/${id}`, { path })).then(toPage),
8652
+ movePage: (id, path) => json(send(`${host}/${id}`, "POST", { path })).then(
8653
+ toPage
8654
+ ),
8636
8655
  /** Publishes one of a page's versions. */
8637
8656
  publishPage: (id, versionId) => json(
8638
- post(`${host}/${id}/publish`, { version: Number(versionId) })
8657
+ send(`${host}/${id}/publish`, "POST", {
8658
+ version: Number(versionId)
8659
+ })
8639
8660
  ).then(toPage),
8640
- unpublishPage: (id) => json(post(`${host}/${id}/unpublish`)).then(toPage),
8641
- deletePage: (id) => json(fetch(`${host}/${id}`, { method: "DELETE" }))
8661
+ unpublishPage: (id) => json(send(`${host}/${id}/unpublish`, "POST")).then(
8662
+ toPage
8663
+ ),
8664
+ deletePage: (id) => json(send(`${host}/${id}`, "DELETE"))
8642
8665
  };
8643
- }, [host]);
8666
+ }, [host, prepareRequest]);
8644
8667
  };
8645
8668
 
8646
8669
  // src/hooks/usePageEditorState.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@puckeditor/plugin-pages",
3
- "version": "0.9.0-canary.f8c1abd5",
3
+ "version": "0.9.0",
4
4
  "author": "Chris Villa <chris@puckeditor.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -27,7 +27,7 @@
27
27
  "dependencies": {
28
28
  "date-fns": "^4.1.0",
29
29
  "fast-equals": "^6.0.0",
30
- "zod": "^4.1.12",
30
+ "zod": "^4.6.7",
31
31
  "zustand": "^5.0.8"
32
32
  },
33
33
  "devDependencies": {
@@ -46,9 +46,9 @@
46
46
  "typescript": "^5.5.4",
47
47
  "vite-tsconfig-paths": "^5.1.4",
48
48
  "vitest": "^4.1.11",
49
- "tsconfig": "0.9.0-canary.f8c1abd5",
50
- "tsup-config": "0.9.0-canary.f8c1abd5",
51
- "eslint-config-custom": "0.9.0-canary.f8c1abd5"
49
+ "tsconfig": "0.9.0",
50
+ "tsup-config": "0.9.0",
51
+ "eslint-config-custom": "0.9.0"
52
52
  },
53
53
  "peerDependencies": {
54
54
  "@puckeditor/core": "^0",