Skip to content

Commit 3d5dd0d

Browse files
committed
implement a few interfaces from the specification
1 parent 592f0cc commit 3d5dd0d

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// https://jsonapi.org/format/#document-resource-object-attributes
2+
//
3+
// The value of the attributes key MUST be an object (an “attributes object”). Members of the attributes object (“attributes”) represent information about the resource object in which it’s defined.
4+
// Attributes may contain any valid JSON value, including complex data structures involving JSON objects and arrays.
5+
// Keys that reference related resources (e.g. author_id) SHOULD NOT appear as attributes. Instead, relationships SHOULD be used.
6+
7+
// Valid JSON values
8+
// https://datatracker.ietf.org/doc/html/rfc8259#section-3
9+
export type JsonValues = object | number | string | false | null | true | Array<JsonValues>;
10+
11+
// https://datatracker.ietf.org/doc/html/rfc8259#section-4
12+
// "A name is a string"
13+
export interface Attributes {
14+
[name: string]: JsonValues;
15+
}
16+
17+
export function isAttributes(attrs: any): attrs is Attributes {
18+
return typeof attrs.attributes === 'object' &&
19+
attrs.attributes !== null &&
20+
!Array.isArray(attrs.attributes)
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://jsonapi.org/format/#document-resource-object-identification
2+
//
3+
// > As noted above, every resource object MUST contain a type member.
4+
// > Every resource object MUST also contain an id member, except when the resource object originates at the client and represents a new resource to be created on the server.
5+
// > If id is omitted due to this exception, a lid member MAY be included to uniquely identify the resource by type locally within the document.
6+
// > The value of the lid member MUST be identical for every representation of the resource in the document, including resource identifier objects.
7+
8+
export interface LocalResourceIdentifier {
9+
lid: string;
10+
type: string;
11+
}
12+
13+
export interface RemoteResourceIdentifier {
14+
id: string;
15+
type: string;
16+
}
17+
18+
export type ResourceIdentifier = LocalResourceIdentifier | RemoteResourceIdentifier;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Attributes } from "./attributes.js";
2+
import { ResourceIdentifier } from "./resource-identifier.js";
3+
4+
type Relationships = void;
5+
type Links = void;
6+
type Meta = void;
7+
8+
export interface _ResourceObject {
9+
attributes?: Attributes;
10+
relationships?: Relationships;
11+
links?: Links;
12+
meta?: Meta;
13+
}
14+
15+
export type ResourceObject = _ResourceObject & ResourceIdentifier;

0 commit comments

Comments
 (0)