Skip to content

Commit 72323b5

Browse files
committed
feat: implement mapper function
1 parent 63ca3a5 commit 72323b5

4 files changed

Lines changed: 104 additions & 11 deletions

File tree

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@
3434
"coverage": "pnpm lint && vitest run --coverage"
3535
},
3636
"devDependencies": {
37+
"@types/lodash": "^4.14.195",
3738
"@vitest/coverage-v8": "0.33.0",
3839
"changelogen": "0.5.4",
3940
"prettier": "3.0.0",
4041
"tsup": "7.1.0",
4142
"typescript": "5.1.6",
4243
"vitest": "0.33.0"
4344
},
44-
"license": "MIT"
45+
"license": "MIT",
46+
"dependencies": {
47+
"lodash": "^4.17.21"
48+
}
4549
}

pnpm-lock.yaml

Lines changed: 22 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,78 @@
1-
import { ShowMessage } from './types';
1+
import { isArray, isObject, has as $has, first as $first, each as $each } from 'lodash';
22

3-
export const showMessage: ShowMessage = (message) => console.log(message);
3+
/**
4+
* Transform the Strapi API response removing the not needed nesting
5+
* @param data object to mapping
6+
* @returns mapped object
7+
*/
48

5-
showMessage('Hello world!');
9+
export function mapperData<T>(data: any): T {
10+
// Single
11+
if ($has(data, 'attributes')) {
12+
return mapperData(removeObjectKey(data, 'attributes'));
13+
}
14+
15+
// collection
16+
if (isArray(data) && data.length && $has($first(data), 'attributes')) {
17+
return data.map((el: any) => mapperData(el)) as T;
18+
}
19+
20+
// Fields
21+
$each(data, (value, key) => {
22+
if (!value) return;
23+
24+
// single
25+
if (isObject(value)) {
26+
data[key] = mapperData(value);
27+
}
28+
29+
// many
30+
if (isArray(value)) {
31+
data[key] = value.map((field) => mapperData(field));
32+
}
33+
34+
// relation(s)
35+
if ($has(value, 'data')) {
36+
let relation = null;
37+
38+
// single
39+
if (isObject(value.data)) {
40+
relation = mapperData(value.data);
41+
}
42+
43+
// many
44+
if (isArray(value.data)) {
45+
relation = value.data.map((el: any) => mapperData(el));
46+
}
47+
48+
data[key] = relation;
49+
}
50+
51+
// single component
52+
if ($has(value, 'id')) {
53+
data[key] = mapperData(value);
54+
}
55+
56+
// repeatable component & dynamic zone
57+
if (isArray(value) && $has($first(value), 'id')) {
58+
data[key] = value.map((p) => mapperData(p));
59+
}
60+
61+
// single media
62+
if ($has(value, 'provider')) {
63+
return;
64+
}
65+
66+
// multiple media
67+
if (isArray(value) && $has($first(value), 'provider')) {
68+
return;
69+
}
70+
});
71+
72+
return data;
73+
}
74+
75+
const removeObjectKey = <T extends { [key: string]: any }>(object: T, key: string): T => ({
76+
id: object.id,
77+
...object[key],
78+
});

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export type ShowMessage = (message: string) => void;
1+
export declare function mapperData<T>(data: any): T;

0 commit comments

Comments
 (0)