Skip to content

Commit 29cacac

Browse files
committed
feat: add Resource class
1 parent d56ac7c commit 29cacac

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export * from './database/Seeder'
22
export * from './database/Factory'
33

4+
export * from './resources/Resource'
5+
46
export * from './decorators/Ignore'
57
export * from './decorators/Options'
68
export * from './decorators/ExtraParams'

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/base",
3-
"version": "1.2.5",
3+
"version": "1.2.6",
44
"license": "MIT",
55
"author": "João Lenon",
66
"repository": "https://github.com/SecJS/Base.git",

resources/Resource.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export function Resource<JsonResource>() {
2+
return class Resource {
3+
static blueprint(model: any) {
4+
return model
5+
}
6+
7+
static toJson(model: any): JsonResource {
8+
if (!model) return null
9+
10+
const blueprint = this.blueprint(model)
11+
12+
Object.keys(blueprint).forEach(key => {
13+
if (!blueprint[key]) delete blueprint[key]
14+
})
15+
16+
return JSON.parse(JSON.stringify(blueprint))
17+
}
18+
19+
static toArray(models: any[]): JsonResource[] {
20+
if (!models) return null
21+
22+
return models.map(model => this.toJson(model))
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)