Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/amauryd/deno-json-api-deserializer
a deserializer for JSON:API payloads
https://github.com/amauryd/deno-json-api-deserializer
backend deno deserializer jsonapi typescript
Last synced: 5 days ago
JSON representation
a deserializer for JSON:API payloads
- Host: GitHub
- URL: https://github.com/amauryd/deno-json-api-deserializer
- Owner: AmauryD
- License: mit
- Created: 2021-09-14T22:21:10.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-09-16T21:25:31.000Z (over 3 years ago)
- Last Synced: 2024-12-16T07:55:35.362Z (7 days ago)
- Topics: backend, deno, deserializer, jsonapi, typescript
- Language: TypeScript
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# deno-json-api-deserializer [![Deno](https://github.com/AmauryD/deno-json-api-deserializer/actions/workflows/deno.yml/badge.svg)](https://github.com/AmauryD/deno-json-api-deserializer/actions/workflows/deno.yml)
A backend deserializer for JSON:API payloads.
## Features
- Deserialize JSON:API payloads
- Async
- Highly modular with transforms functions
- Filtering attributes/relationships## Examples
Simple use
```typescript
import { JsonApiDeserializer } from "...";const deserializer = new JsonApiDeserializer();
await desserializer.deserialize({
data: {
attributes: {
title: "test",
},
},
});
```camelCase deserializer with transforms
```typescript
const deserializer = new JsonApiDeserializer();async function main() {
/**
* For every transformation, we apply the camelCase function on the key
*/
deserializer.transformAttributeFunction = (
key,
value,
) => [camelCase(key), value];
deserializer.transformManyRelationshipFunction = (
key,
value,
) => [camelCase(key), value.map((e) => e.id)];
deserializer.transformOneRelationshipFunction = (
key,
value,
) => [camelCase(key), value];const deserialized = await deserializer.deserialize({
data: {
attributes: {
title_test: "test",
},
relationships: {
people_comments: {
data: [
{ type: "comments", id: "9" },
{ type: "comments", id: "10" },
],
},
},
},
});
// { id: undefined, peopleComments: [ "9", "10" ], titleTest: "test" }
}
```Filtering
```typescript
import { JsonApiDeserializer } from "...";// allowed elements
const whitelist = ["title", "content"];async function main() {
const deserializer = new JsonApiDeserializer();/**
* when a transform returns undefined instead of a tuple, the property is skipped
*/
deserializer.transformAttributeFunction = (
key,
value,
) => whitelist.includes(key) ? [key, value] : undefined;const deserialized = await deserializer.deserialize({
data: {
attributes: {
title: "test",
content: "test",
unknownAttribute: "blah",
},
},
});// { id: undefined, title: "test", content: "test" }
}
```