payload-intl 1.0.0 → 1.1.1

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.
@@ -0,0 +1,11 @@
1
+ export declare const ENDPOINT_CONFIG: {
2
+ setMessages: {
3
+ path: string;
4
+ method: "put";
5
+ };
6
+ getMessages: {
7
+ path: string;
8
+ method: "get";
9
+ };
10
+ };
11
+ //# sourceMappingURL=const.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"const.d.ts","sourceRoot":"","sources":["../src/const.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,eAAe;;;;;;;;;CASiC,CAAC"}
package/dist/const.js ADDED
@@ -0,0 +1,14 @@
1
+ const t = {
2
+ setMessages: {
3
+ path: "/intl-plugin",
4
+ method: "put"
5
+ },
6
+ getMessages: {
7
+ path: "/intl-plugin/:locale",
8
+ method: "get"
9
+ }
10
+ };
11
+ export {
12
+ t as ENDPOINT_CONFIG
13
+ };
14
+ //# sourceMappingURL=const.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"const.js","sources":["../src/const.ts"],"sourcesContent":["import type { Endpoint } from 'payload';\n\nexport const ENDPOINT_CONFIG = {\n setMessages: {\n path: '/intl-plugin',\n method: 'put',\n },\n getMessages: {\n path: '/intl-plugin/:locale',\n method: 'get',\n },\n} satisfies Record<string, Pick<Endpoint, 'path' | 'method'>>;\n"],"names":["ENDPOINT_CONFIG"],"mappings":"AAEO,MAAMA,IAAkB;AAAA,EAC7B,aAAa;AAAA,IACX,MAAM;AAAA,IACN,QAAQ;AAAA,EAAA;AAAA,EAEV,aAAa;AAAA,IACX,MAAM;AAAA,IACN,QAAQ;AAAA,EAAA;AAEZ;"}
@@ -1 +1 @@
1
- {"version":3,"file":"get-messages.d.ts","sourceRoot":"","sources":["../../src/endpoints/get-messages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAkB,MAAM,SAAS,CAAC;AAIxD,eAAO,MAAM,mBAAmB,EAAE,QAQjC,CAAC"}
1
+ {"version":3,"file":"get-messages.d.ts","sourceRoot":"","sources":["../../src/endpoints/get-messages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAkB,MAAM,SAAS,CAAC;AAIxD,eAAO,MAAM,mBAAmB,EAAE,QAOjC,CAAC"}
@@ -1,13 +1,13 @@
1
- import { fetchMessages as a } from "../requests/fetchMessages.js";
2
- const n = {
3
- handler: async (e) => {
4
- const { locale: s } = e.routeParams, t = await a(e.payload, s);
5
- return Response.json(t);
6
- },
7
- method: "get",
8
- path: "/intl-plugin/:locale"
1
+ import { ENDPOINT_CONFIG as a } from "../const.js";
2
+ import { fetchMessages as t } from "../requests/fetchMessages.js";
3
+ const c = {
4
+ ...a.getMessages,
5
+ handler: async (s) => {
6
+ const { locale: e } = s.routeParams, o = await t(s.payload, e);
7
+ return Response.json(o);
8
+ }
9
9
  };
10
10
  export {
11
- n as getMessagesEndpoint
11
+ c as getMessagesEndpoint
12
12
  };
13
13
  //# sourceMappingURL=get-messages.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"get-messages.js","sources":["../../src/endpoints/get-messages.ts"],"sourcesContent":["import type { Endpoint, PayloadRequest } from 'payload';\n\nimport { fetchMessages } from '../requests/fetchMessages';\n\nexport const getMessagesEndpoint: Endpoint = {\n handler: async (req: PayloadRequest) => {\n const { locale } = req.routeParams as { locale: string };\n const messages = await fetchMessages(req.payload, locale);\n return Response.json(messages);\n },\n method: 'get',\n path: '/intl-plugin/:locale',\n};\n"],"names":["getMessagesEndpoint","req","locale","messages","fetchMessages"],"mappings":";AAIO,MAAMA,IAAgC;AAAA,EAC3C,SAAS,OAAOC,MAAwB;AACtC,UAAM,EAAE,QAAAC,MAAWD,EAAI,aACjBE,IAAW,MAAMC,EAAcH,EAAI,SAASC,CAAM;AACxD,WAAO,SAAS,KAAKC,CAAQ;AAAA,EAC/B;AAAA,EACA,QAAQ;AAAA,EACR,MAAM;AACR;"}
1
+ {"version":3,"file":"get-messages.js","sources":["../../src/endpoints/get-messages.ts"],"sourcesContent":["import type { Endpoint, PayloadRequest } from 'payload';\nimport { ENDPOINT_CONFIG } from '@/const';\nimport { fetchMessages } from '../requests/fetchMessages';\n\nexport const getMessagesEndpoint: Endpoint = {\n ...ENDPOINT_CONFIG.getMessages,\n handler: async (req: PayloadRequest) => {\n const { locale } = req.routeParams as { locale: string };\n const messages = await fetchMessages(req.payload, locale);\n return Response.json(messages);\n },\n};\n"],"names":["getMessagesEndpoint","ENDPOINT_CONFIG","req","locale","messages","fetchMessages"],"mappings":";;AAIO,MAAMA,IAAgC;AAAA,EAC3C,GAAGC,EAAgB;AAAA,EACnB,SAAS,OAAOC,MAAwB;AACtC,UAAM,EAAE,QAAAC,MAAWD,EAAI,aACjBE,IAAW,MAAMC,EAAcH,EAAI,SAASC,CAAM;AACxD,WAAO,SAAS,KAAKC,CAAQ;AAAA,EAC/B;AACF;"}
@@ -1 +1 @@
1
- {"version":3,"file":"set-messages.d.ts","sourceRoot":"","sources":["../../src/endpoints/set-messages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAwB,MAAM,SAAS,CAAC;AAK9D,eAAO,MAAM,mBAAmB,EAAE,QAuDjC,CAAC"}
1
+ {"version":3,"file":"set-messages.d.ts","sourceRoot":"","sources":["../../src/endpoints/set-messages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAwB,MAAM,SAAS,CAAC;AAK9D,eAAO,MAAM,mBAAmB,EAAE,QAsDjC,CAAC"}
@@ -1,47 +1,47 @@
1
- import { getSupportedLocales as p, getPluginContext as d } from "../utils/config.js";
2
- const f = {
3
- handler: async (a) => {
4
- const { user: c } = await a.payload.auth({ headers: a.headers });
1
+ import { ENDPOINT_CONFIG as d } from "../const.js";
2
+ import { getSupportedLocales as p, getPluginContext as f } from "../utils/config.js";
3
+ const g = {
4
+ ...d.setMessages,
5
+ handler: async (o) => {
6
+ const { user: c } = await o.payload.auth({ headers: o.headers });
5
7
  if (!c)
6
8
  throw new Error("Unauthorized");
7
- const e = await a.json?.();
8
- if (!e)
9
+ const t = await o.json?.();
10
+ if (!t)
9
11
  throw new Error("No data provided");
10
12
  const l = p(
11
- a.payload.config.localization
12
- ), { collectionSlug: n } = d(a.payload.config);
13
- for (const o of l) {
14
- const s = e[o];
15
- if (!s) continue;
16
- const t = new File(
17
- [JSON.stringify(s)],
18
- `${o}-${Date.now()}.json`,
13
+ o.payload.config.localization
14
+ ), { collectionSlug: s } = f(o.payload.config);
15
+ for (const a of l) {
16
+ const n = t[a];
17
+ if (!n) continue;
18
+ const e = new File(
19
+ [JSON.stringify(n)],
20
+ `${a}-${Date.now()}.json`,
19
21
  {
20
22
  type: "application/json"
21
23
  }
22
24
  ), i = {
23
- name: t.name,
24
- data: Buffer.from(await t.arrayBuffer()),
25
- mimetype: t.type,
26
- size: t.size
27
- }, { docs: r } = await a.payload.update({
28
- collection: n,
25
+ name: e.name,
26
+ data: Buffer.from(await e.arrayBuffer()),
27
+ mimetype: e.type,
28
+ size: e.size
29
+ }, { docs: r } = await o.payload.update({
30
+ collection: s,
29
31
  data: {},
30
32
  file: i,
31
- where: { locale: { equals: o } }
33
+ where: { locale: { equals: a } }
32
34
  });
33
- r.length === 0 && await a.payload.create({
34
- collection: n,
35
- data: { locale: o },
35
+ r.length === 0 && await o.payload.create({
36
+ collection: s,
37
+ data: { locale: a },
36
38
  file: i
37
39
  });
38
40
  }
39
41
  return Response.json({ success: !0 });
40
- },
41
- method: "put",
42
- path: "/intl-plugin"
42
+ }
43
43
  };
44
44
  export {
45
- f as setMessagesEndpoint
45
+ g as setMessagesEndpoint
46
46
  };
47
47
  //# sourceMappingURL=set-messages.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"set-messages.js","sources":["../../src/endpoints/set-messages.ts"],"sourcesContent":["import type { Endpoint, File, PayloadRequest } from 'payload';\nimport type { Messages, Translations } from '@/types';\n\nimport { getPluginContext, getSupportedLocales } from '@/utils/config';\n\nexport const setMessagesEndpoint: Endpoint = {\n handler: async (req: PayloadRequest) => {\n const { user } = await req.payload.auth({ headers: req.headers });\n if (!user) {\n throw new Error('Unauthorized');\n }\n\n const data = (await req.json?.()) as Translations<Messages> | undefined;\n if (!data) {\n throw new Error('No data provided');\n }\n\n const supportedLocales = getSupportedLocales(\n req.payload.config.localization,\n );\n const { collectionSlug } = getPluginContext(req.payload.config);\n\n for (const locale of supportedLocales) {\n const messages = data[locale];\n if (!messages) continue;\n\n const rawFile = new File(\n [JSON.stringify(messages)],\n `${locale}-${Date.now()}.json`,\n {\n type: 'application/json',\n },\n );\n\n const file: File = {\n name: rawFile.name,\n data: Buffer.from(await rawFile.arrayBuffer()),\n mimetype: rawFile.type,\n size: rawFile.size,\n };\n\n const { docs } = await req.payload.update({\n collection: collectionSlug,\n data: {},\n file,\n where: { locale: { equals: locale } },\n });\n if (docs.length === 0) {\n await req.payload.create({\n collection: collectionSlug,\n data: { locale },\n file,\n });\n }\n }\n\n return Response.json({ success: true });\n },\n method: 'put',\n path: '/intl-plugin',\n};\n"],"names":["setMessagesEndpoint","req","user","data","supportedLocales","getSupportedLocales","collectionSlug","getPluginContext","locale","messages","rawFile","file","docs"],"mappings":";AAKO,MAAMA,IAAgC;AAAA,EAC3C,SAAS,OAAOC,MAAwB;AACtC,UAAM,EAAE,MAAAC,MAAS,MAAMD,EAAI,QAAQ,KAAK,EAAE,SAASA,EAAI,SAAS;AAChE,QAAI,CAACC;AACH,YAAM,IAAI,MAAM,cAAc;AAGhC,UAAMC,IAAQ,MAAMF,EAAI,OAAA;AACxB,QAAI,CAACE;AACH,YAAM,IAAI,MAAM,kBAAkB;AAGpC,UAAMC,IAAmBC;AAAA,MACvBJ,EAAI,QAAQ,OAAO;AAAA,IAAA,GAEf,EAAE,gBAAAK,EAAA,IAAmBC,EAAiBN,EAAI,QAAQ,MAAM;AAE9D,eAAWO,KAAUJ,GAAkB;AACrC,YAAMK,IAAWN,EAAKK,CAAM;AAC5B,UAAI,CAACC,EAAU;AAEf,YAAMC,IAAU,IAAI;AAAA,QAClB,CAAC,KAAK,UAAUD,CAAQ,CAAC;AAAA,QACzB,GAAGD,CAAM,IAAI,KAAK,KAAK;AAAA,QACvB;AAAA,UACE,MAAM;AAAA,QAAA;AAAA,MACR,GAGIG,IAAa;AAAA,QACjB,MAAMD,EAAQ;AAAA,QACd,MAAM,OAAO,KAAK,MAAMA,EAAQ,aAAa;AAAA,QAC7C,UAAUA,EAAQ;AAAA,QAClB,MAAMA,EAAQ;AAAA,MAAA,GAGV,EAAE,MAAAE,EAAA,IAAS,MAAMX,EAAI,QAAQ,OAAO;AAAA,QACxC,YAAYK;AAAA,QACZ,MAAM,CAAA;AAAA,QACN,MAAAK;AAAA,QACA,OAAO,EAAE,QAAQ,EAAE,QAAQH,IAAO;AAAA,MAAE,CACrC;AACD,MAAII,EAAK,WAAW,KAClB,MAAMX,EAAI,QAAQ,OAAO;AAAA,QACvB,YAAYK;AAAA,QACZ,MAAM,EAAE,QAAAE,EAAA;AAAA,QACR,MAAAG;AAAA,MAAA,CACD;AAAA,IAEL;AAEA,WAAO,SAAS,KAAK,EAAE,SAAS,IAAM;AAAA,EACxC;AAAA,EACA,QAAQ;AAAA,EACR,MAAM;AACR;"}
1
+ {"version":3,"file":"set-messages.js","sources":["../../src/endpoints/set-messages.ts"],"sourcesContent":["import type { Endpoint, File, PayloadRequest } from 'payload';\nimport { ENDPOINT_CONFIG } from '@/const';\nimport type { Messages, Translations } from '@/types';\nimport { getPluginContext, getSupportedLocales } from '@/utils/config';\n\nexport const setMessagesEndpoint: Endpoint = {\n ...ENDPOINT_CONFIG.setMessages,\n handler: async (req: PayloadRequest) => {\n const { user } = await req.payload.auth({ headers: req.headers });\n if (!user) {\n throw new Error('Unauthorized');\n }\n\n const data = (await req.json?.()) as Translations<Messages> | undefined;\n if (!data) {\n throw new Error('No data provided');\n }\n\n const supportedLocales = getSupportedLocales(\n req.payload.config.localization,\n );\n const { collectionSlug } = getPluginContext(req.payload.config);\n\n for (const locale of supportedLocales) {\n const messages = data[locale];\n if (!messages) continue;\n\n const rawFile = new File(\n [JSON.stringify(messages)],\n `${locale}-${Date.now()}.json`,\n {\n type: 'application/json',\n },\n );\n\n const file: File = {\n name: rawFile.name,\n data: Buffer.from(await rawFile.arrayBuffer()),\n mimetype: rawFile.type,\n size: rawFile.size,\n };\n\n const { docs } = await req.payload.update({\n collection: collectionSlug,\n data: {},\n file,\n where: { locale: { equals: locale } },\n });\n if (docs.length === 0) {\n await req.payload.create({\n collection: collectionSlug,\n data: { locale },\n file,\n });\n }\n }\n\n return Response.json({ success: true });\n },\n};\n"],"names":["setMessagesEndpoint","ENDPOINT_CONFIG","req","user","data","supportedLocales","getSupportedLocales","collectionSlug","getPluginContext","locale","messages","rawFile","file","docs"],"mappings":";;AAKO,MAAMA,IAAgC;AAAA,EAC3C,GAAGC,EAAgB;AAAA,EACnB,SAAS,OAAOC,MAAwB;AACtC,UAAM,EAAE,MAAAC,MAAS,MAAMD,EAAI,QAAQ,KAAK,EAAE,SAASA,EAAI,SAAS;AAChE,QAAI,CAACC;AACH,YAAM,IAAI,MAAM,cAAc;AAGhC,UAAMC,IAAQ,MAAMF,EAAI,OAAA;AACxB,QAAI,CAACE;AACH,YAAM,IAAI,MAAM,kBAAkB;AAGpC,UAAMC,IAAmBC;AAAA,MACvBJ,EAAI,QAAQ,OAAO;AAAA,IAAA,GAEf,EAAE,gBAAAK,EAAA,IAAmBC,EAAiBN,EAAI,QAAQ,MAAM;AAE9D,eAAWO,KAAUJ,GAAkB;AACrC,YAAMK,IAAWN,EAAKK,CAAM;AAC5B,UAAI,CAACC,EAAU;AAEf,YAAMC,IAAU,IAAI;AAAA,QAClB,CAAC,KAAK,UAAUD,CAAQ,CAAC;AAAA,QACzB,GAAGD,CAAM,IAAI,KAAK,KAAK;AAAA,QACvB;AAAA,UACE,MAAM;AAAA,QAAA;AAAA,MACR,GAGIG,IAAa;AAAA,QACjB,MAAMD,EAAQ;AAAA,QACd,MAAM,OAAO,KAAK,MAAMA,EAAQ,aAAa;AAAA,QAC7C,UAAUA,EAAQ;AAAA,QAClB,MAAMA,EAAQ;AAAA,MAAA,GAGV,EAAE,MAAAE,EAAA,IAAS,MAAMX,EAAI,QAAQ,OAAO;AAAA,QACxC,YAAYK;AAAA,QACZ,MAAM,CAAA;AAAA,QACN,MAAAK;AAAA,QACA,OAAO,EAAE,QAAQ,EAAE,QAAQH,IAAO;AAAA,MAAE,CACrC;AACD,MAAII,EAAK,WAAW,KAClB,MAAMX,EAAI,QAAQ,OAAO;AAAA,QACvB,YAAYK;AAAA,QACZ,MAAM,EAAE,QAAAE,EAAA;AAAA,QACR,MAAAG;AAAA,MAAA,CACD;AAAA,IAEL;AAEA,WAAO,SAAS,KAAK,EAAE,SAAS,IAAM;AAAA,EACxC;AACF;"}
@@ -1,4 +1,14 @@
1
1
  import { BasePayload } from 'payload';
2
2
  import { Messages } from '../types';
3
+ interface MessagesRequestConfig {
4
+ serverUrl: string;
5
+ /**
6
+ * If defined a custom api route in your payload.config.ts (`routes.api`), add them same here.
7
+ * @default 'api'
8
+ */
9
+ apiRoute?: string;
10
+ }
11
+ export declare function fetchMessages(config: MessagesRequestConfig, locale: string): Promise<Messages>;
3
12
  export declare function fetchMessages(payload: BasePayload, locale: string): Promise<Messages>;
13
+ export {};
4
14
  //# sourceMappingURL=fetchMessages.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fetchMessages.d.ts","sourceRoot":"","sources":["../../src/requests/fetchMessages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAK3C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEzC,wBAAsB,aAAa,CACjC,OAAO,EAAE,WAAW,EACpB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,QAAQ,CAAC,CAqCnB"}
1
+ {"version":3,"file":"fetchMessages.d.ts","sourceRoot":"","sources":["../../src/requests/fetchMessages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAI3C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEzC,UAAU,qBAAqB;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,wBAAsB,aAAa,CACjC,MAAM,EAAE,qBAAqB,EAC7B,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,QAAQ,CAAC,CAAC;AACrB,wBAAsB,aAAa,CACjC,OAAO,EAAE,WAAW,EACpB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,QAAQ,CAAC,CAAC"}
@@ -1,32 +1,41 @@
1
- import { getPluginContext as i } from "../utils/config.js";
2
- import { getErrorMessage as c } from "../utils/error-handling.js";
3
- async function l(r, o) {
4
- if (!r.config.serverURL)
5
- throw new Error(
6
- "serverURL is required in your payload.config.ts file for payload-intl to work."
1
+ import { ENDPOINT_CONFIG as u } from "../const.js";
2
+ import { getPluginContext as h } from "../utils/config.js";
3
+ import { getErrorMessage as w } from "../utils/error-handling.js";
4
+ async function N(t, e) {
5
+ if ("serverUrl" in t) {
6
+ const { serverUrl: r, apiRoute: l = "api" } = t, f = l.replace(/^\/|\/$/g, ""), g = u.getMessages.path.replace(/^\/|\/$/g, "").replace(":locale", e), p = `/${f}/${g}`, c = new URL(p, r);
7
+ console.debug(
8
+ `PAYLOAD_INTL: Fetching messages from API: ${c.toString()}`
7
9
  );
8
- const {
9
- docs: [t]
10
- } = await r.find({
11
- collection: i(r.config).collectionSlug,
12
- where: { locale: { equals: o } }
10
+ const i = await fetch(c.toString());
11
+ if (!i.ok)
12
+ throw new Error(`Failed to fetch messages for locale ${e}`);
13
+ return await i.json();
14
+ }
15
+ const s = t, {
16
+ docs: [n]
17
+ } = await s.find({
18
+ collection: h(s.config).collectionSlug,
19
+ where: { locale: { equals: e } }
13
20
  });
14
- if (!t)
15
- return console.warn(`No messages found for locale ${o}`), {};
16
- const { url: n } = t, e = await fetch(n);
17
- if (!e.ok) {
18
- const s = await c(e);
21
+ if (!n)
22
+ return console.warn(`No messages found for locale ${e}`), {};
23
+ const { url: a } = n;
24
+ console.debug(`PAYLOAD_INTL: Fetching messages from stroage: ${a}`);
25
+ const o = await fetch(a);
26
+ if (!o.ok) {
27
+ const r = await w(o);
19
28
  throw new Error(
20
- `Could not fetch messages for locale "${o}": ${s}`
29
+ `Could not fetch messages for locale "${e}": ${r}`
21
30
  );
22
31
  }
23
- if (e.headers.get("content-type") !== "application/json")
32
+ if (o.headers.get("content-type") !== "application/json")
24
33
  throw new Error(
25
- `Could not fetch messages for locale "${o}": The page did not return a JSON file.`
34
+ `Could not fetch messages for locale "${e}": The page did not return a JSON file.`
26
35
  );
27
- return await e.json();
36
+ return await o.json();
28
37
  }
29
38
  export {
30
- l as fetchMessages
39
+ N as fetchMessages
31
40
  };
32
41
  //# sourceMappingURL=fetchMessages.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"fetchMessages.js","sources":["../../src/requests/fetchMessages.ts"],"sourcesContent":["import type { BasePayload } from 'payload';\n\nimport { getPluginContext } from '@/utils/config';\nimport { getErrorMessage } from '@/utils/error-handling';\n\nimport type { Messages } from '../types';\n\nexport async function fetchMessages(\n payload: BasePayload,\n locale: string,\n): Promise<Messages> {\n if (!payload.config.serverURL) {\n throw new Error(\n 'serverURL is required in your payload.config.ts file for payload-intl to work.',\n );\n }\n\n const {\n docs: [doc],\n } = await payload.find({\n collection: getPluginContext(payload.config).collectionSlug,\n where: { locale: { equals: locale } },\n });\n\n if (!doc) {\n console.warn(`No messages found for locale ${locale}`);\n return {};\n }\n\n const { url } = doc as unknown as { url: string };\n\n const response = await fetch(url);\n\n if (!response.ok) {\n const error = await getErrorMessage(response);\n throw new Error(\n `Could not fetch messages for locale \"${locale}\": ${error}`,\n );\n }\n\n if (response.headers.get('content-type') !== 'application/json') {\n throw new Error(\n `Could not fetch messages for locale \"${locale}\": The page did not return a JSON file.`,\n );\n }\n\n return await response.json();\n}\n\n/*\ntry {\n const response = await fetch(endpointUrl, {\n method: \"PUT\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(changes),\n });\n\n if (!response.ok) {\n const error = await response.json().catch(() => ({}));\n throw new Error(\n \"message\" in error ? error.message : \"Unknown error\"\n );\n }\n\n form.reset(currentValues);\n toast.success(\"Saved\", { id: toastId });\n } catch (error) {\n toast.error(\n `Failed to save: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n { id: toastId },\n );\n }\n };\n\n*/\n"],"names":["fetchMessages","payload","locale","doc","getPluginContext","url","response","error","getErrorMessage"],"mappings":";;AAOA,eAAsBA,EACpBC,GACAC,GACmB;AACnB,MAAI,CAACD,EAAQ,OAAO;AAClB,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAIJ,QAAM;AAAA,IACJ,MAAM,CAACE,CAAG;AAAA,EAAA,IACR,MAAMF,EAAQ,KAAK;AAAA,IACrB,YAAYG,EAAiBH,EAAQ,MAAM,EAAE;AAAA,IAC7C,OAAO,EAAE,QAAQ,EAAE,QAAQC,IAAO;AAAA,EAAE,CACrC;AAED,MAAI,CAACC;AACH,mBAAQ,KAAK,gCAAgCD,CAAM,EAAE,GAC9C,CAAA;AAGT,QAAM,EAAE,KAAAG,MAAQF,GAEVG,IAAW,MAAM,MAAMD,CAAG;AAEhC,MAAI,CAACC,EAAS,IAAI;AAChB,UAAMC,IAAQ,MAAMC,EAAgBF,CAAQ;AAC5C,UAAM,IAAI;AAAA,MACR,wCAAwCJ,CAAM,MAAMK,CAAK;AAAA,IAAA;AAAA,EAE7D;AAEA,MAAID,EAAS,QAAQ,IAAI,cAAc,MAAM;AAC3C,UAAM,IAAI;AAAA,MACR,wCAAwCJ,CAAM;AAAA,IAAA;AAIlD,SAAO,MAAMI,EAAS,KAAA;AACxB;"}
1
+ {"version":3,"file":"fetchMessages.js","sources":["../../src/requests/fetchMessages.ts"],"sourcesContent":["import type { BasePayload } from 'payload';\nimport { ENDPOINT_CONFIG } from '@/const';\nimport { getPluginContext } from '@/utils/config';\nimport { getErrorMessage } from '@/utils/error-handling';\nimport type { Messages } from '../types';\n\ninterface MessagesRequestConfig {\n serverUrl: string;\n /**\n * If defined a custom api route in your payload.config.ts (`routes.api`), add them same here.\n * @default 'api'\n */\n apiRoute?: string;\n}\n\nexport async function fetchMessages(\n config: MessagesRequestConfig,\n locale: string,\n): Promise<Messages>;\nexport async function fetchMessages(\n payload: BasePayload,\n locale: string,\n): Promise<Messages>;\nexport async function fetchMessages(\n configOrPayload: MessagesRequestConfig | BasePayload,\n locale: string,\n): Promise<Messages> {\n if ('serverUrl' in configOrPayload) {\n const { serverUrl, apiRoute = 'api' } = configOrPayload;\n\n const cleanApiRoute = apiRoute.replace(/^\\/|\\/$/g, '');\n\n const relativePath = ENDPOINT_CONFIG.getMessages.path\n .replace(/^\\/|\\/$/g, '')\n .replace(':locale', locale);\n\n const fullPath = `/${cleanApiRoute}/${relativePath}`;\n\n const url = new URL(fullPath, serverUrl);\n\n console.debug(\n `PAYLOAD_INTL: Fetching messages from API: ${url.toString()}`,\n );\n\n const response = await fetch(url.toString());\n if (!response.ok) {\n throw new Error(`Failed to fetch messages for locale ${locale}`);\n }\n return await response.json();\n }\n\n const payload = configOrPayload;\n\n const {\n docs: [doc],\n } = await payload.find({\n collection: getPluginContext(payload.config).collectionSlug,\n where: { locale: { equals: locale } },\n });\n\n if (!doc) {\n console.warn(`No messages found for locale ${locale}`);\n return {};\n }\n\n const { url } = doc as unknown as { url: string };\n\n console.debug(`PAYLOAD_INTL: Fetching messages from stroage: ${url}`);\n\n const response = await fetch(url);\n\n if (!response.ok) {\n const error = await getErrorMessage(response);\n throw new Error(\n `Could not fetch messages for locale \"${locale}\": ${error}`,\n );\n }\n\n if (response.headers.get('content-type') !== 'application/json') {\n throw new Error(\n `Could not fetch messages for locale \"${locale}\": The page did not return a JSON file.`,\n );\n }\n\n return await response.json();\n}\n"],"names":["fetchMessages","configOrPayload","locale","serverUrl","apiRoute","cleanApiRoute","relativePath","ENDPOINT_CONFIG","fullPath","url","response","payload","doc","getPluginContext","error","getErrorMessage"],"mappings":";;;AAuBA,eAAsBA,EACpBC,GACAC,GACmB;AACnB,MAAI,eAAeD,GAAiB;AAClC,UAAM,EAAE,WAAAE,GAAW,UAAAC,IAAW,MAAA,IAAUH,GAElCI,IAAgBD,EAAS,QAAQ,YAAY,EAAE,GAE/CE,IAAeC,EAAgB,YAAY,KAC9C,QAAQ,YAAY,EAAE,EACtB,QAAQ,WAAWL,CAAM,GAEtBM,IAAW,IAAIH,CAAa,IAAIC,CAAY,IAE5CG,IAAM,IAAI,IAAID,GAAUL,CAAS;AAEvC,YAAQ;AAAA,MACN,6CAA6CM,EAAI,UAAU;AAAA,IAAA;AAG7D,UAAMC,IAAW,MAAM,MAAMD,EAAI,UAAU;AAC3C,QAAI,CAACC,EAAS;AACZ,YAAM,IAAI,MAAM,uCAAuCR,CAAM,EAAE;AAEjE,WAAO,MAAMQ,EAAS,KAAA;AAAA,EACxB;AAEA,QAAMC,IAAUV,GAEV;AAAA,IACJ,MAAM,CAACW,CAAG;AAAA,EAAA,IACR,MAAMD,EAAQ,KAAK;AAAA,IACrB,YAAYE,EAAiBF,EAAQ,MAAM,EAAE;AAAA,IAC7C,OAAO,EAAE,QAAQ,EAAE,QAAQT,IAAO;AAAA,EAAE,CACrC;AAED,MAAI,CAACU;AACH,mBAAQ,KAAK,gCAAgCV,CAAM,EAAE,GAC9C,CAAA;AAGT,QAAM,EAAE,KAAAO,MAAQG;AAEhB,UAAQ,MAAM,iDAAiDH,CAAG,EAAE;AAEpE,QAAMC,IAAW,MAAM,MAAMD,CAAG;AAEhC,MAAI,CAACC,EAAS,IAAI;AAChB,UAAMI,IAAQ,MAAMC,EAAgBL,CAAQ;AAC5C,UAAM,IAAI;AAAA,MACR,wCAAwCR,CAAM,MAAMY,CAAK;AAAA,IAAA;AAAA,EAE7D;AAEA,MAAIJ,EAAS,QAAQ,IAAI,cAAc,MAAM;AAC3C,UAAM,IAAI;AAAA,MACR,wCAAwCR,CAAM;AAAA,IAAA;AAIlD,SAAO,MAAMQ,EAAS,KAAA;AACxB;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payload-intl",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "Payload Plugin for I18N using ICU Messages",
5
5
  "keywords": [
6
6
  "payload",
@@ -99,10 +99,8 @@
99
99
  "pnpm": "^9 || ^10"
100
100
  },
101
101
  "publishConfig": {
102
- "access": "public",
103
- "provenance": true
102
+ "access": "public"
104
103
  },
105
- "registry": "https://registry.npmjs.org/",
106
104
  "scripts": {
107
105
  "prebuild": "pnpm typecheck",
108
106
  "build": "vite build",