@webstudio-is/sdk 0.228.0 → 0.230.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/lib/index.js CHANGED
@@ -254,11 +254,6 @@ var Method = z5.union([
254
254
  z5.literal("put"),
255
255
  z5.literal("delete")
256
256
  ]);
257
- var Header = z5.object({
258
- name: z5.string(),
259
- // expression
260
- value: z5.string()
261
- });
262
257
  var Resource = z5.object({
263
258
  id: ResourceId,
264
259
  name: z5.string(),
@@ -266,16 +261,41 @@ var Resource = z5.object({
266
261
  method: Method,
267
262
  // expression
268
263
  url: z5.string(),
269
- headers: z5.array(Header),
264
+ searchParams: z5.array(
265
+ z5.object({
266
+ name: z5.string(),
267
+ // expression
268
+ value: z5.string()
269
+ })
270
+ ).optional(),
271
+ headers: z5.array(
272
+ z5.object({
273
+ name: z5.string(),
274
+ // expression
275
+ value: z5.string()
276
+ })
277
+ ),
270
278
  // expression
271
279
  body: z5.optional(z5.string())
272
280
  });
273
281
  var ResourceRequest = z5.object({
274
- id: ResourceId,
275
282
  name: z5.string(),
276
283
  method: Method,
277
284
  url: z5.string(),
278
- headers: z5.array(Header),
285
+ searchParams: z5.array(
286
+ z5.object({
287
+ name: z5.string(),
288
+ // can be string or object which should be serialized
289
+ value: z5.unknown()
290
+ })
291
+ ),
292
+ headers: z5.array(
293
+ z5.object({
294
+ name: z5.string(),
295
+ // can be string or object which should be serialized
296
+ value: z5.unknown()
297
+ })
298
+ ),
279
299
  body: z5.optional(z5.unknown())
280
300
  });
281
301
  var Resources = z5.map(ResourceId, Resource);
@@ -2310,8 +2330,6 @@ var generateResources = ({
2310
2330
  let generatedRequest = "";
2311
2331
  const resourceName = scope.getName(resource.id, resource.name);
2312
2332
  generatedRequest += ` const ${resourceName}: ResourceRequest = {
2313
- `;
2314
- generatedRequest += ` id: "${resource.id}",
2315
2333
  `;
2316
2334
  generatedRequest += ` name: ${JSON.stringify(resource.name)},
2317
2335
  `;
@@ -2322,6 +2340,20 @@ var generateResources = ({
2322
2340
  scope
2323
2341
  });
2324
2342
  generatedRequest += ` url: ${url},
2343
+ `;
2344
+ generatedRequest += ` searchParams: [
2345
+ `;
2346
+ for (const searchParam of resource.searchParams ?? []) {
2347
+ const value = generateExpression({
2348
+ expression: searchParam.value,
2349
+ dataSources,
2350
+ usedDataSources,
2351
+ scope
2352
+ });
2353
+ generatedRequest += ` { name: "${searchParam.name}", value: ${value} },
2354
+ `;
2355
+ }
2356
+ generatedRequest += ` ],
2325
2357
  `;
2326
2358
  generatedRequest += ` method: "${resource.method}",
2327
2359
  `;
package/lib/runtime.js CHANGED
@@ -1,3 +1,33 @@
1
+ // src/resource-loader.ts
2
+ import hash from "@emotion/hash";
3
+
4
+ // src/to-string.ts
5
+ var createJsonStringifyProxy = (target) => {
6
+ return new Proxy(target, {
7
+ get(target2, prop, receiver) {
8
+ if (prop === "toString") {
9
+ return function() {
10
+ return JSON.stringify(target2);
11
+ };
12
+ }
13
+ const value = Reflect.get(target2, prop, receiver);
14
+ if (typeof value === "object" && value !== null) {
15
+ return createJsonStringifyProxy(value);
16
+ }
17
+ return value;
18
+ }
19
+ });
20
+ };
21
+ var isPlainObject = (value) => {
22
+ return Object.prototype.toString.call(value) === "[object Object]" && (Object.getPrototypeOf(value) === null || Object.getPrototypeOf(value) === Object.prototype);
23
+ };
24
+ var serializeValue = (value) => {
25
+ if (typeof value === "string") {
26
+ return value;
27
+ }
28
+ return JSON.stringify(value);
29
+ };
30
+
1
31
  // src/resource-loader.ts
2
32
  var LOCAL_RESOURCE_PREFIX = "$resources";
3
33
  var isLocalResource = (pathname, resourceName) => {
@@ -9,24 +39,33 @@ var isLocalResource = (pathname, resourceName) => {
9
39
  };
10
40
  var sitemapResourceUrl = `/${LOCAL_RESOURCE_PREFIX}/sitemap.xml`;
11
41
  var loadResource = async (customFetch, resourceRequest) => {
12
- const { url, method, headers, body } = resourceRequest;
13
- const requestHeaders = new Headers(
14
- headers.map(({ name, value }) => [name, value])
15
- );
16
- const requestInit = {
17
- method,
18
- headers: requestHeaders
19
- };
20
- if (method !== "get" && body !== void 0) {
21
- if (typeof body === "string") {
22
- requestInit.body = body;
42
+ try {
43
+ const { method, searchParams, headers, body } = resourceRequest;
44
+ let href = resourceRequest.url;
45
+ try {
46
+ const url = new URL(resourceRequest.url.trim());
47
+ if (searchParams) {
48
+ for (const { name, value } of searchParams) {
49
+ url.searchParams.append(name, serializeValue(value));
50
+ }
51
+ }
52
+ href = url.href;
53
+ } catch {
23
54
  }
24
- if (typeof body === "object") {
25
- requestInit.body = JSON.stringify(body);
55
+ const requestHeaders = new Headers(
56
+ headers.map(({ name, value }) => [
57
+ name,
58
+ serializeValue(value)
59
+ ])
60
+ );
61
+ const requestInit = {
62
+ method,
63
+ headers: requestHeaders
64
+ };
65
+ if (method !== "get" && body !== void 0) {
66
+ requestInit.body = serializeValue(body);
26
67
  }
27
- }
28
- try {
29
- const response = await customFetch(url.trim(), requestInit);
68
+ const response = await customFetch(href, requestInit);
30
69
  let data = await response.text();
31
70
  try {
32
71
  data = JSON.parse(data);
@@ -34,14 +73,14 @@ var loadResource = async (customFetch, resourceRequest) => {
34
73
  }
35
74
  if (!response.ok) {
36
75
  console.error(
37
- `Failed to load resource: ${url} - ${response.status}: ${JSON.stringify(data).slice(0, 300)}`
76
+ `Failed to load resource: ${href} - ${response.status}: ${JSON.stringify(data).slice(0, 300)}`
38
77
  );
39
78
  }
40
79
  return {
41
80
  ok: response.ok,
42
- data,
43
81
  status: response.status,
44
- statusText: response.statusText
82
+ statusText: response.statusText,
83
+ data
45
84
  };
46
85
  } catch (error) {
47
86
  console.error(error);
@@ -64,26 +103,38 @@ var loadResources = async (customFetch, requests) => {
64
103
  )
65
104
  );
66
105
  };
67
-
68
- // src/to-string.ts
69
- var createJsonStringifyProxy = (target) => {
70
- return new Proxy(target, {
71
- get(target2, prop, receiver) {
72
- if (prop === "toString") {
73
- return function() {
74
- return JSON.stringify(target2);
75
- };
76
- }
77
- const value = Reflect.get(target2, prop, receiver);
78
- if (typeof value === "object" && value !== null) {
79
- return createJsonStringifyProxy(value);
80
- }
81
- return value;
82
- }
83
- });
106
+ var getCacheKey = async (request) => {
107
+ const url = new URL(request.url);
108
+ const method = request.method;
109
+ const body = await request.clone().text();
110
+ const cacheControl = request.headers.get("Cache-Control");
111
+ const resourceHash = hash(`${method}:${body}:${cacheControl}`);
112
+ url.searchParams.set("ws-resource-hash", resourceHash);
113
+ return url;
84
114
  };
85
- var isPlainObject = (value) => {
86
- return Object.prototype.toString.call(value) === "[object Object]" && (Object.getPrototypeOf(value) === null || Object.getPrototypeOf(value) === Object.prototype);
115
+ var cachedFetch = async (namespace, input, init) => {
116
+ if (globalThis.caches) {
117
+ const request = new Request(input, init);
118
+ const requestCacheControl = request.headers.get("Cache-Control");
119
+ if (!requestCacheControl) {
120
+ return fetch(input, init);
121
+ }
122
+ const cache = await caches.open(namespace);
123
+ const cacheKey = await getCacheKey(request);
124
+ let response = await cache.match(cacheKey);
125
+ if (response) {
126
+ return new Response(response.body, response);
127
+ }
128
+ response = await fetch(request);
129
+ if (!response.ok) {
130
+ return response;
131
+ }
132
+ response = new Response(response.body, response);
133
+ response.headers.set("Cache-Control", requestCacheControl);
134
+ await cache.put(cacheKey, response.clone());
135
+ return response;
136
+ }
137
+ return fetch(input, init);
87
138
  };
88
139
 
89
140
  // src/form-fields.ts
@@ -102,6 +153,7 @@ var getIndexWithinAncestorFromProps = (props) => {
102
153
  var animationCanPlayOnCanvasProperty = "data-ws-animation-can-play-on-canvas";
103
154
  export {
104
155
  animationCanPlayOnCanvasProperty,
156
+ cachedFetch,
105
157
  createJsonStringifyProxy,
106
158
  formBotFieldName,
107
159
  formIdFieldName,
@@ -112,6 +164,7 @@ export {
112
164
  isPlainObject,
113
165
  loadResource,
114
166
  loadResources,
167
+ serializeValue,
115
168
  sitemapResourceUrl,
116
169
  tagProperty
117
170
  };
@@ -6,9 +6,9 @@ export declare const isLocalResource: (pathname: string, resourceName?: string)
6
6
  export declare const sitemapResourceUrl = "/$resources/sitemap.xml";
7
7
  export declare const loadResource: (customFetch: typeof fetch, resourceRequest: ResourceRequest) => Promise<{
8
8
  ok: boolean;
9
- data: string;
10
9
  status: number;
11
10
  statusText: string;
11
+ data: string;
12
12
  } | {
13
13
  ok: boolean;
14
14
  data: undefined;
@@ -18,9 +18,9 @@ export declare const loadResource: (customFetch: typeof fetch, resourceRequest:
18
18
  export declare const loadResources: (customFetch: typeof fetch, requests: Map<string, ResourceRequest>) => Promise<{
19
19
  [k: string]: {
20
20
  ok: boolean;
21
- data: string;
22
21
  status: number;
23
22
  statusText: string;
23
+ data: string;
24
24
  } | {
25
25
  ok: boolean;
26
26
  data: undefined;
@@ -28,3 +28,4 @@ export declare const loadResources: (customFetch: typeof fetch, requests: Map<st
28
28
  statusText: string;
29
29
  };
30
30
  }>;
31
+ export declare const cachedFetch: (namespace: string, input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
@@ -5,6 +5,16 @@ export declare const Resource: z.ZodObject<{
5
5
  control: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"system">, z.ZodLiteral<"graphql">]>>;
6
6
  method: z.ZodUnion<[z.ZodLiteral<"get">, z.ZodLiteral<"post">, z.ZodLiteral<"put">, z.ZodLiteral<"delete">]>;
7
7
  url: z.ZodString;
8
+ searchParams: z.ZodOptional<z.ZodArray<z.ZodObject<{
9
+ name: z.ZodString;
10
+ value: z.ZodString;
11
+ }, "strip", z.ZodTypeAny, {
12
+ value: string;
13
+ name: string;
14
+ }, {
15
+ value: string;
16
+ name: string;
17
+ }>, "many">>;
8
18
  headers: z.ZodArray<z.ZodObject<{
9
19
  name: z.ZodString;
10
20
  value: z.ZodString;
@@ -27,6 +37,10 @@ export declare const Resource: z.ZodObject<{
27
37
  }[];
28
38
  body?: string | undefined;
29
39
  control?: "system" | "graphql" | undefined;
40
+ searchParams?: {
41
+ value: string;
42
+ name: string;
43
+ }[] | undefined;
30
44
  }, {
31
45
  name: string;
32
46
  url: string;
@@ -38,42 +52,61 @@ export declare const Resource: z.ZodObject<{
38
52
  }[];
39
53
  body?: string | undefined;
40
54
  control?: "system" | "graphql" | undefined;
55
+ searchParams?: {
56
+ value: string;
57
+ name: string;
58
+ }[] | undefined;
41
59
  }>;
42
60
  export type Resource = z.infer<typeof Resource>;
43
61
  export declare const ResourceRequest: z.ZodObject<{
44
- id: z.ZodString;
45
62
  name: z.ZodString;
46
63
  method: z.ZodUnion<[z.ZodLiteral<"get">, z.ZodLiteral<"post">, z.ZodLiteral<"put">, z.ZodLiteral<"delete">]>;
47
64
  url: z.ZodString;
65
+ searchParams: z.ZodArray<z.ZodObject<{
66
+ name: z.ZodString;
67
+ value: z.ZodUnknown;
68
+ }, "strip", z.ZodTypeAny, {
69
+ name: string;
70
+ value?: unknown;
71
+ }, {
72
+ name: string;
73
+ value?: unknown;
74
+ }>, "many">;
48
75
  headers: z.ZodArray<z.ZodObject<{
49
76
  name: z.ZodString;
50
- value: z.ZodString;
77
+ value: z.ZodUnknown;
51
78
  }, "strip", z.ZodTypeAny, {
52
- value: string;
53
79
  name: string;
80
+ value?: unknown;
54
81
  }, {
55
- value: string;
56
82
  name: string;
83
+ value?: unknown;
57
84
  }>, "many">;
58
85
  body: z.ZodOptional<z.ZodUnknown>;
59
86
  }, "strip", z.ZodTypeAny, {
60
87
  name: string;
61
88
  url: string;
62
- id: string;
63
89
  method: "get" | "post" | "put" | "delete";
90
+ searchParams: {
91
+ name: string;
92
+ value?: unknown;
93
+ }[];
64
94
  headers: {
65
- value: string;
66
95
  name: string;
96
+ value?: unknown;
67
97
  }[];
68
98
  body?: unknown;
69
99
  }, {
70
100
  name: string;
71
101
  url: string;
72
- id: string;
73
102
  method: "get" | "post" | "put" | "delete";
103
+ searchParams: {
104
+ name: string;
105
+ value?: unknown;
106
+ }[];
74
107
  headers: {
75
- value: string;
76
108
  name: string;
109
+ value?: unknown;
77
110
  }[];
78
111
  body?: unknown;
79
112
  }>;
@@ -84,6 +117,16 @@ export declare const Resources: z.ZodMap<z.ZodString, z.ZodObject<{
84
117
  control: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"system">, z.ZodLiteral<"graphql">]>>;
85
118
  method: z.ZodUnion<[z.ZodLiteral<"get">, z.ZodLiteral<"post">, z.ZodLiteral<"put">, z.ZodLiteral<"delete">]>;
86
119
  url: z.ZodString;
120
+ searchParams: z.ZodOptional<z.ZodArray<z.ZodObject<{
121
+ name: z.ZodString;
122
+ value: z.ZodString;
123
+ }, "strip", z.ZodTypeAny, {
124
+ value: string;
125
+ name: string;
126
+ }, {
127
+ value: string;
128
+ name: string;
129
+ }>, "many">>;
87
130
  headers: z.ZodArray<z.ZodObject<{
88
131
  name: z.ZodString;
89
132
  value: z.ZodString;
@@ -106,6 +149,10 @@ export declare const Resources: z.ZodMap<z.ZodString, z.ZodObject<{
106
149
  }[];
107
150
  body?: string | undefined;
108
151
  control?: "system" | "graphql" | undefined;
152
+ searchParams?: {
153
+ value: string;
154
+ name: string;
155
+ }[] | undefined;
109
156
  }, {
110
157
  name: string;
111
158
  url: string;
@@ -117,5 +164,9 @@ export declare const Resources: z.ZodMap<z.ZodString, z.ZodObject<{
117
164
  }[];
118
165
  body?: string | undefined;
119
166
  control?: "system" | "graphql" | undefined;
167
+ searchParams?: {
168
+ value: string;
169
+ name: string;
170
+ }[] | undefined;
120
171
  }>>;
121
172
  export type Resources = z.infer<typeof Resources>;
@@ -402,6 +402,16 @@ export declare const WebstudioFragment: z.ZodObject<{
402
402
  control: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"system">, z.ZodLiteral<"graphql">]>>;
403
403
  method: z.ZodUnion<[z.ZodLiteral<"get">, z.ZodLiteral<"post">, z.ZodLiteral<"put">, z.ZodLiteral<"delete">]>;
404
404
  url: z.ZodString;
405
+ searchParams: z.ZodOptional<z.ZodArray<z.ZodObject<{
406
+ name: z.ZodString;
407
+ value: z.ZodString;
408
+ }, "strip", z.ZodTypeAny, {
409
+ value: string;
410
+ name: string;
411
+ }, {
412
+ value: string;
413
+ name: string;
414
+ }>, "many">>;
405
415
  headers: z.ZodArray<z.ZodObject<{
406
416
  name: z.ZodString;
407
417
  value: z.ZodString;
@@ -424,6 +434,10 @@ export declare const WebstudioFragment: z.ZodObject<{
424
434
  }[];
425
435
  body?: string | undefined;
426
436
  control?: "system" | "graphql" | undefined;
437
+ searchParams?: {
438
+ value: string;
439
+ name: string;
440
+ }[] | undefined;
427
441
  }, {
428
442
  name: string;
429
443
  url: string;
@@ -435,6 +449,10 @@ export declare const WebstudioFragment: z.ZodObject<{
435
449
  }[];
436
450
  body?: string | undefined;
437
451
  control?: "system" | "graphql" | undefined;
452
+ searchParams?: {
453
+ value: string;
454
+ name: string;
455
+ }[] | undefined;
438
456
  }>, "many">;
439
457
  props: z.ZodArray<z.ZodUnion<[z.ZodObject<{
440
458
  type: z.ZodLiteral<"number">;
@@ -22544,6 +22562,10 @@ export declare const WebstudioFragment: z.ZodObject<{
22544
22562
  }[];
22545
22563
  body?: string | undefined;
22546
22564
  control?: "system" | "graphql" | undefined;
22565
+ searchParams?: {
22566
+ value: string;
22567
+ name: string;
22568
+ }[] | undefined;
22547
22569
  }[];
22548
22570
  breakpoints: {
22549
22571
  label: string;
@@ -24704,6 +24726,10 @@ export declare const WebstudioFragment: z.ZodObject<{
24704
24726
  }[];
24705
24727
  body?: string | undefined;
24706
24728
  control?: "system" | "graphql" | undefined;
24729
+ searchParams?: {
24730
+ value: string;
24731
+ name: string;
24732
+ }[] | undefined;
24707
24733
  }[];
24708
24734
  breakpoints: {
24709
24735
  label: string;
@@ -1,2 +1,3 @@
1
1
  export declare const createJsonStringifyProxy: <T extends object>(target: T) => T;
2
2
  export declare const isPlainObject: (value: unknown) => value is object;
3
+ export declare const serializeValue: (value: unknown) => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/sdk",
3
- "version": "0.228.0",
3
+ "version": "0.230.0",
4
4
  "description": "Webstudio project data schema",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -34,22 +34,23 @@
34
34
  ],
35
35
  "sideEffects": false,
36
36
  "dependencies": {
37
+ "@emotion/hash": "^0.9.2",
37
38
  "acorn": "^8.14.1",
38
39
  "acorn-walk": "^8.3.4",
39
40
  "change-case": "^5.4.4",
40
41
  "reserved-identifiers": "^1.0.0",
41
42
  "type-fest": "^4.37.0",
42
43
  "zod": "^3.24.2",
43
- "@webstudio-is/css-engine": "0.228.0",
44
- "@webstudio-is/fonts": "0.228.0",
45
- "@webstudio-is/icons": "0.228.0"
44
+ "@webstudio-is/css-engine": "0.230.0",
45
+ "@webstudio-is/fonts": "0.230.0",
46
+ "@webstudio-is/icons": "0.230.0"
46
47
  },
47
48
  "devDependencies": {
48
49
  "html-tags": "^4.0.0",
49
50
  "vitest": "^3.1.2",
51
+ "@webstudio-is/template": "0.230.0",
50
52
  "@webstudio-is/css-data": "0.0.0",
51
- "@webstudio-is/tsconfig": "1.0.7",
52
- "@webstudio-is/template": "0.228.0"
53
+ "@webstudio-is/tsconfig": "1.0.7"
53
54
  },
54
55
  "scripts": {
55
56
  "typecheck": "tsc",