@primate/core 0.8.1 → 0.8.3

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.
@@ -12,54 +12,43 @@ export default function i18n(config) {
12
12
  const default_catalog = catalogs[config.defaultLocale];
13
13
  const currency = config.currency ?? "USD";
14
14
  const locales = Object.keys(catalogs);
15
- const get_locale = () => {
15
+ function get() {
16
16
  const store = server_storage().getStore();
17
17
  return store?.locale ?? config.defaultLocale;
18
- };
18
+ }
19
+ ;
20
+ function set(_locale) {
21
+ throw new Error("[i18n] locale.set is not supported on server.");
22
+ }
23
+ ;
24
+ function translate(locale) {
25
+ return (...args) => {
26
+ const formatter = new Formatter(locale);
27
+ const [key, params = {}] = args;
28
+ const translated = resolve(catalogs[locale], key) ??
29
+ resolve(default_catalog, key) ??
30
+ String(key);
31
+ return (is.string(translated)
32
+ ? format(translated, params, currency, formatter)
33
+ : translated);
34
+ };
35
+ }
19
36
  function t(...args) {
20
- const active_locale = get_locale();
21
- const formatter = new Formatter(active_locale);
22
- const [key, params] = args;
23
- const translated = resolve(catalogs[active_locale], key) ??
24
- resolve(default_catalog, key) ??
25
- String(key);
26
- if (is.string(translated)) {
27
- return format(translated, params ?? {}, currency, formatter);
28
- }
29
- return translated;
37
+ return translate(get())(...args);
30
38
  }
31
39
  const api = t;
32
- Object.defineProperties(api, {
33
- defaultLocale: {
34
- value: config.defaultLocale,
35
- enumerable: true,
36
- },
37
- locales: {
38
- value: locales,
39
- enumerable: true,
40
- },
41
- catalogs: {
42
- value: catalogs,
43
- enumerable: true,
44
- },
45
- currency: {
46
- value: currency,
47
- enumerable: true,
48
- },
49
- locale: {
50
- value: {
51
- get: get_locale,
52
- set: (_locale) => {
53
- throw new Error("[i18n] locale.set is not supported on server.");
54
- },
55
- },
56
- enumerable: true,
57
- },
58
- restore: {
59
- value: () => { },
60
- enumerable: true,
61
- },
62
- });
40
+ api.defaultLocale = config.defaultLocale;
41
+ api.locales = locales;
42
+ api.catalogs = catalogs;
43
+ api.currency = currency;
44
+ api.locale = { get, set };
45
+ api.restore = () => { };
46
+ api.with = (locale) => {
47
+ if (!(locale in catalogs)) {
48
+ throw new Error(`[i18n] Unknown locale "${locale}".`);
49
+ }
50
+ return translate(locale);
51
+ };
63
52
  return api;
64
53
  }
65
54
  //# sourceMappingURL=config.server.js.map
@@ -16,21 +16,24 @@ type Path<O extends RouteOptions> = O extends {
16
16
  type MethodMeta = {
17
17
  contentType?: string;
18
18
  };
19
+ type RequestMeta = {
20
+ headers?: HeadersInit;
21
+ };
19
22
  type ClientMethod<O extends RouteOptions, R = unknown> = MethodMeta & {
20
23
  _result?: R;
21
- } & (Body<O> extends never ? Path<O> extends never ? () => Promise<Response> : (args: {
24
+ } & (Body<O> extends never ? Path<O> extends never ? (args?: RequestMeta) => Promise<Response> : (args: {
22
25
  path: Path<O>;
23
- }) => Promise<Response> : Path<O> extends never ? (args: {
26
+ } & RequestMeta) => Promise<Response> : Path<O> extends never ? (args: {
24
27
  body: Body<O>;
25
- }) => Promise<Response> : (args: {
28
+ } & RequestMeta) => Promise<Response> : (args: {
26
29
  body: Body<O>;
27
30
  path: Path<O>;
28
- }) => Promise<Response>);
31
+ } & RequestMeta) => Promise<Response>);
29
32
  type ClientRoute<R> = {
30
33
  [K in keyof R]: R[K] extends {
31
34
  options: infer O extends RouteOptions;
32
35
  result?: infer Result;
33
- } ? ClientMethod<O, Result> : () => Promise<Response>;
36
+ } ? ClientMethod<O, Result> : (args?: RequestMeta) => Promise<Response>;
34
37
  };
35
38
  type WithResult<O extends RouteOptions, R = unknown> = {
36
39
  handler: RouteHandler<O>;
@@ -38,7 +38,10 @@ function route(handlers) {
38
38
  : path;
39
39
  return fetch(resolved, {
40
40
  method: method.toUpperCase(),
41
- headers: headers(contentType),
41
+ headers: {
42
+ ...headers(contentType),
43
+ ...(args.headers ?? {}),
44
+ },
42
45
  body: serialize_body(contentType, args.body),
43
46
  });
44
47
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primate/core",
3
- "version": "0.8.1",
3
+ "version": "0.8.3",
4
4
  "description": "The universal web framework",
5
5
  "homepage": "https://primate.run",
6
6
  "bugs": "https://github.com/primate-run/primate/issues",