@appsemble/utils 0.37.0 → 0.37.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.
package/README.md CHANGED
@@ -1,9 +1,9 @@
1
- # ![](https://gitlab.com/appsemble/appsemble/-/raw/0.37.0/config/assets/logo.svg) Appsemble Utilities
1
+ # ![](https://gitlab.com/appsemble/appsemble/-/raw/0.37.1/config/assets/logo.svg) Appsemble Utilities
2
2
 
3
3
  > Internal utility functions used across multiple Appsemble projects.
4
4
 
5
5
  [![npm](https://img.shields.io/npm/v/@appsemble/utils)](https://www.npmjs.com/package/@appsemble/utils)
6
- [![GitLab CI](https://gitlab.com/appsemble/appsemble/badges/0.37.0/pipeline.svg)](https://gitlab.com/appsemble/appsemble/-/releases/0.37.0)
6
+ [![GitLab CI](https://gitlab.com/appsemble/appsemble/badges/0.37.1/pipeline.svg)](https://gitlab.com/appsemble/appsemble/-/releases/0.37.1)
7
7
  [![Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://prettier.io)
8
8
 
9
9
  ## Table of Contents
@@ -26,5 +26,5 @@ not guaranteed.
26
26
 
27
27
  ## License
28
28
 
29
- [LGPL-3.0-only](https://gitlab.com/appsemble/appsemble/-/blob/0.37.0/LICENSE.md) ©
29
+ [LGPL-3.0-only](https://gitlab.com/appsemble/appsemble/-/blob/0.37.1/LICENSE.md) ©
30
30
  [Appsemble](https://appsemble.com)
@@ -19,6 +19,19 @@ export const pathItems = {
19
19
  schema: {
20
20
  type: 'object',
21
21
  additionalProperties: true,
22
+ properties: {
23
+ assets: {
24
+ type: 'array',
25
+ items: {
26
+ type: 'string',
27
+ format: 'binary',
28
+ },
29
+ },
30
+ resource: {
31
+ type: 'object',
32
+ additionalProperties: true,
33
+ },
34
+ },
22
35
  },
23
36
  },
24
37
  'application/json': {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@appsemble/utils",
3
- "version": "0.37.0",
3
+ "version": "0.37.1",
4
4
  "description": "Utility functions used in Appsemble internally",
5
5
  "keywords": [
6
6
  "app",
@@ -39,8 +39,8 @@
39
39
  "test": "vitest"
40
40
  },
41
41
  "dependencies": {
42
- "@appsemble/lang-sdk": "0.37.0",
43
- "@appsemble/types": "0.37.0",
42
+ "@appsemble/lang-sdk": "0.37.1",
43
+ "@appsemble/types": "0.37.1",
44
44
  "@fortawesome/fontawesome-common-types": "^6.0.0",
45
45
  "axios": "^1.0.0",
46
46
  "langmap": "^0.0.16",
@@ -7,3 +7,4 @@ import { type JsonValue } from 'type-fest';
7
7
  * referenced from the resource
8
8
  */
9
9
  export declare function serializeResource(data: any): FormData | JsonValue;
10
+ export declare function deserializeResource(data: any, schema?: unknown): any;
@@ -34,4 +34,32 @@ export function serializeResource(data) {
34
34
  }
35
35
  return form;
36
36
  }
37
+ function asResourceSchema(schema) {
38
+ return schema && typeof schema === 'object' ? schema : undefined;
39
+ }
40
+ export function deserializeResource(data, schema) {
41
+ const resource = typeof data.resource === 'string' ? JSON.parse(data.resource) : data.resource;
42
+ const assets = Array.isArray(data.assets) ? data.assets : [data.assets];
43
+ const replaceAssets = (value, valueSchema) => {
44
+ const currentSchema = asResourceSchema(valueSchema);
45
+ if (Array.isArray(value)) {
46
+ const itemSchema = Array.isArray(currentSchema?.items) ? undefined : currentSchema?.items;
47
+ return value.map((item) => replaceAssets(item, itemSchema));
48
+ }
49
+ if (typeof value === 'string' &&
50
+ /^\d+$/.test(value) &&
51
+ (!schema || currentSchema?.format === 'binary')) {
52
+ return assets[Number(value)];
53
+ }
54
+ if (value && typeof value === 'object') {
55
+ const result = {};
56
+ for (const [key, item] of Object.entries(value)) {
57
+ result[key] = replaceAssets(item, currentSchema?.properties?.[key]);
58
+ }
59
+ return result;
60
+ }
61
+ return value;
62
+ };
63
+ return replaceAssets(resource, schema);
64
+ }
37
65
  //# sourceMappingURL=serializeResource.js.map