From d496c9b96a94a7d1e476b7144313c1db9343d173 Mon Sep 17 00:00:00 2001 From: Ryan Huellen Date: Sat, 9 Nov 2024 19:15:28 -0600 Subject: [PATCH] feat: added JSON types --- src/decorators/meta.ts | 2 +- src/types/array.ts | 4 ---- src/types/attributesObject.ts | 4 ++-- src/types/dataTypes.ts | 5 ----- src/types/errorObject.ts | 6 +++--- src/types/index.ts | 5 +---- src/types/json/array.ts | 4 ++++ src/types/json/dataTypes.ts | 5 +++++ src/types/json/index.ts | 4 ++++ src/types/json/object.ts | 3 +++ src/types/json/primitives.ts | 1 + src/types/jsonApiObject.ts | 8 ++++++++ src/types/linkObject.ts | 4 ++-- src/types/linksObject.ts | 4 ++-- src/types/metaObject.ts | 4 ++-- src/types/object.ts | 3 --- src/types/primitives.ts | 1 - src/types/relationshipObject.ts | 4 ++-- src/types/relationshipsObject.ts | 4 ++-- src/types/resourceIdentifierObject.ts | 4 ++-- src/types/resourceObject.ts | 4 ++-- src/types/topLevelObject.ts | 11 ++++------- 22 files changed, 50 insertions(+), 44 deletions(-) delete mode 100644 src/types/array.ts delete mode 100644 src/types/dataTypes.ts create mode 100644 src/types/json/array.ts create mode 100644 src/types/json/dataTypes.ts create mode 100644 src/types/json/index.ts create mode 100644 src/types/json/object.ts create mode 100644 src/types/json/primitives.ts create mode 100644 src/types/jsonApiObject.ts delete mode 100644 src/types/object.ts delete mode 100644 src/types/primitives.ts diff --git a/src/decorators/meta.ts b/src/decorators/meta.ts index 4c4ca64..7ae9193 100644 --- a/src/decorators/meta.ts +++ b/src/decorators/meta.ts @@ -1,7 +1,7 @@ import { buildSymbol } from '../utils/buildSymbol'; import { isValidKey } from '../utils/isValidKey'; -import type { JSONAPIDataTypes } from '../types/dataTypes'; +import type { JSONAPIDataTypes } from '../types/json/dataTypes'; export const metaSymbol = buildSymbol('meta'); diff --git a/src/types/array.ts b/src/types/array.ts deleted file mode 100644 index 1f02765..0000000 --- a/src/types/array.ts +++ /dev/null @@ -1,4 +0,0 @@ -import type { JSONAPIObject } from './object'; -import type { JSONAPIPrimitives } from './primitives'; - -export type JSONAPIArray = JSONAPIObject[] | JSONAPIPrimitives[]; diff --git a/src/types/attributesObject.ts b/src/types/attributesObject.ts index 7ed039a..b1c8494 100644 --- a/src/types/attributesObject.ts +++ b/src/types/attributesObject.ts @@ -1,3 +1,3 @@ -import type { JSONAPIObject } from "./object"; +import type { JSONObject } from "./json/object"; -export type JSONAPIAttributesObject = JSONAPIObject; \ No newline at end of file +export type JSONAPIAttributesObject = JSONObject; \ No newline at end of file diff --git a/src/types/dataTypes.ts b/src/types/dataTypes.ts deleted file mode 100644 index af96dc9..0000000 --- a/src/types/dataTypes.ts +++ /dev/null @@ -1,5 +0,0 @@ -import type { JSONAPIArray } from './array'; -import type { JSONAPIObject } from './object'; -import type { JSONAPIPrimitives } from './primitives'; - -export type JSONAPIDataTypes = JSONAPIPrimitives | JSONAPIArray | JSONAPIObject; diff --git a/src/types/errorObject.ts b/src/types/errorObject.ts index 623609f..6649f1b 100644 --- a/src/types/errorObject.ts +++ b/src/types/errorObject.ts @@ -1,7 +1,7 @@ import type { JSONAPILinkObject } from "./linkObject"; import type { JSONAPILinksObject } from "./linksObject"; import type { JSONAPIMetaObject } from "./metaObject"; -import type { JSONAPIObject } from "./object"; +import type { JSONObject } from "./json/object"; import type { Satisfies } from "./helpers/satisfies"; export type JSONAPIErrorObject = Satisfies<{ @@ -15,9 +15,9 @@ export type JSONAPIErrorObject = Satisfies<{ title: string; detail: string; source: { - pointer: JSONAPIObject; + pointer: JSONObject; parameter: string; header: string; } meta: JSONAPIMetaObject; -}, JSONAPIObject>; \ No newline at end of file +}, JSONObject>; \ No newline at end of file diff --git a/src/types/index.ts b/src/types/index.ts index eee2f49..00d1136 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,11 +1,8 @@ -export * from './array'; +export * from './json'; export * from './attributesObject'; -export * from './dataTypes'; export * from './linkObject'; export * from './linksObject'; export * from './metaObject'; -export * from './object'; -export * from './primitives'; export * from './relationshipsObject'; export * from './relationshipObject'; export * from './resourceObject'; diff --git a/src/types/json/array.ts b/src/types/json/array.ts new file mode 100644 index 0000000..7885f04 --- /dev/null +++ b/src/types/json/array.ts @@ -0,0 +1,4 @@ +import type { JSONObject } from './object'; +import type { JSONPrimitives } from './primitives'; + +export type JSONArray = JSONObject[] | JSONPrimitives[]; diff --git a/src/types/json/dataTypes.ts b/src/types/json/dataTypes.ts new file mode 100644 index 0000000..364d172 --- /dev/null +++ b/src/types/json/dataTypes.ts @@ -0,0 +1,5 @@ +import type { JSONArray } from './array'; +import type { JSONObject } from './object'; +import type { JSONPrimitives } from './primitives'; + +export type JSONDataTypes = JSONPrimitives | JSONArray | JSONObject; diff --git a/src/types/json/index.ts b/src/types/json/index.ts new file mode 100644 index 0000000..f079d09 --- /dev/null +++ b/src/types/json/index.ts @@ -0,0 +1,4 @@ +export * from './dataTypes'; +export * from './primitives'; +export * from './object'; +export * from './array'; \ No newline at end of file diff --git a/src/types/json/object.ts b/src/types/json/object.ts new file mode 100644 index 0000000..a4e247b --- /dev/null +++ b/src/types/json/object.ts @@ -0,0 +1,3 @@ +import type { JSONDataTypes } from './dataTypes'; + +export type JSONObject = { [key: string]: JSONDataTypes }; diff --git a/src/types/json/primitives.ts b/src/types/json/primitives.ts new file mode 100644 index 0000000..47b3621 --- /dev/null +++ b/src/types/json/primitives.ts @@ -0,0 +1 @@ +export type JSONPrimitives = string | number | boolean | null; diff --git a/src/types/jsonApiObject.ts b/src/types/jsonApiObject.ts new file mode 100644 index 0000000..03b7194 --- /dev/null +++ b/src/types/jsonApiObject.ts @@ -0,0 +1,8 @@ +import type { Satisfies } from "./helpers/satisfies"; +import type { JSONObject } from "./json/object"; + +export type JSONAPIObject = Satisfies<{ + verison?: string; + ext?: string[]; + profile?: string[]; +}, JSONObject>; \ No newline at end of file diff --git a/src/types/linkObject.ts b/src/types/linkObject.ts index 75f1d19..d0d7e1f 100644 --- a/src/types/linkObject.ts +++ b/src/types/linkObject.ts @@ -1,5 +1,5 @@ import type { JSONAPIMetaObject } from './metaObject'; -import type { JSONAPIObject } from './object'; +import type { JSONObject } from './json/object'; import type { Satisfies } from './helpers/satisfies'; export type JSONAPILinkObject = Satisfies< @@ -12,5 +12,5 @@ export type JSONAPILinkObject = Satisfies< hreflang?: string; meta?: JSONAPIMetaObject; }, - JSONAPIObject + JSONObject >; diff --git a/src/types/linksObject.ts b/src/types/linksObject.ts index 97b9d6e..d006913 100644 --- a/src/types/linksObject.ts +++ b/src/types/linksObject.ts @@ -1,4 +1,4 @@ -import type { JSONAPIObject } from "./object"; +import type { JSONObject } from "./json/object"; import type { Satisfies } from "./helpers/satisfies"; -export type JSONAPILinksObject = Satisfies<{ [key: string]: string | JSONAPILinksObject | null }, JSONAPIObject>; \ No newline at end of file +export type JSONAPILinksObject = Satisfies<{ [key: string]: string | JSONAPILinksObject | null }, JSONObject>; \ No newline at end of file diff --git a/src/types/metaObject.ts b/src/types/metaObject.ts index 0fbc087..7d72fd0 100644 --- a/src/types/metaObject.ts +++ b/src/types/metaObject.ts @@ -1,3 +1,3 @@ -import type { JSONAPIObject } from './object'; +import type { JSONObject } from './json/object'; -export type JSONAPIMetaObject = JSONAPIObject; +export type JSONAPIMetaObject = JSONObject; diff --git a/src/types/object.ts b/src/types/object.ts deleted file mode 100644 index 6177cca..0000000 --- a/src/types/object.ts +++ /dev/null @@ -1,3 +0,0 @@ -import type { JSONAPIDataTypes } from './dataTypes'; - -export type JSONAPIObject = { [key: string]: JSONAPIDataTypes }; diff --git a/src/types/primitives.ts b/src/types/primitives.ts deleted file mode 100644 index 6cb5546..0000000 --- a/src/types/primitives.ts +++ /dev/null @@ -1 +0,0 @@ -export type JSONAPIPrimitives = string | number | boolean | null; diff --git a/src/types/relationshipObject.ts b/src/types/relationshipObject.ts index c6a3dbc..5deb39f 100644 --- a/src/types/relationshipObject.ts +++ b/src/types/relationshipObject.ts @@ -1,11 +1,11 @@ import type { JSONAPIResourceIdentifierObject } from "./resourceIdentifierObject"; import type { JSONAPILinksObject } from "./linksObject"; import type { JSONAPIMetaObject } from "./metaObject"; -import type { JSONAPIObject } from "./object"; +import type { JSONObject } from "./json/object"; import type { Satisfies } from "./helpers/satisfies"; export type JSONAPIRelationshipObject = Satisfies<{ data: JSONAPIResourceIdentifierObject; links?: JSONAPILinksObject; meta?: JSONAPIMetaObject; -}, JSONAPIObject>; \ No newline at end of file +}, JSONObject>; \ No newline at end of file diff --git a/src/types/relationshipsObject.ts b/src/types/relationshipsObject.ts index 8bd6318..34da670 100644 --- a/src/types/relationshipsObject.ts +++ b/src/types/relationshipsObject.ts @@ -1,5 +1,5 @@ -import type { JSONAPIObject } from "./object"; +import type { JSONObject } from "./json/object"; import type { Satisfies } from "./helpers/satisfies"; import type { JSONAPIRelationshipObject } from "./relationshipObject"; -export type JSONAPIRelationshipsObject = Satisfies<{ [key: string]: JSONAPIRelationshipObject }, JSONAPIObject>; \ No newline at end of file +export type JSONAPIRelationshipsObject = Satisfies<{ [key: string]: JSONAPIRelationshipObject }, JSONObject>; \ No newline at end of file diff --git a/src/types/resourceIdentifierObject.ts b/src/types/resourceIdentifierObject.ts index b37bbea..badab18 100644 --- a/src/types/resourceIdentifierObject.ts +++ b/src/types/resourceIdentifierObject.ts @@ -1,4 +1,4 @@ -import type { JSONAPIObject } from "./object"; +import type { JSONObject } from "./json/object"; import type { Satisfies } from "./helpers/satisfies"; export type JSONAPIResourceIdentifierObject = Satisfies<{ @@ -6,4 +6,4 @@ export type JSONAPIResourceIdentifierObject = Satisfies<{ } & ( { id: string } | { lid: string } -), JSONAPIObject>; \ No newline at end of file +), JSONObject>; \ No newline at end of file diff --git a/src/types/resourceObject.ts b/src/types/resourceObject.ts index 30b8c0d..b7a457b 100644 --- a/src/types/resourceObject.ts +++ b/src/types/resourceObject.ts @@ -1,7 +1,7 @@ import type { JSONAPIAttributesObject } from "./attributesObject"; import type { JSONAPILinksObject } from "./linksObject"; import type { JSONAPIMetaObject } from "./metaObject"; -import type { JSONAPIObject } from "./object"; +import type { JSONObject } from "./json/object"; import type { JSONAPIRelationshipsObject } from "./relationshipsObject"; import type { JSONAPILinkObject } from "./linkObject"; import type { Satisfies } from "./helpers/satisfies"; @@ -17,4 +17,4 @@ export type JSONAPIResourceObject = Satisfies< }; meta?: JSONAPIMetaObject; } -, JSONAPIObject>; \ No newline at end of file +, JSONObject>; \ No newline at end of file diff --git a/src/types/topLevelObject.ts b/src/types/topLevelObject.ts index 9c43576..38252ec 100644 --- a/src/types/topLevelObject.ts +++ b/src/types/topLevelObject.ts @@ -1,12 +1,13 @@ import type { JSONAPIErrorObject } from "./errorObject"; import type { JSONAPILinksObject } from "./linksObject"; import type { JSONAPIMetaObject } from "./metaObject"; -import type { JSONAPIObject } from "./object"; +import type { JSONObject } from "./json/object"; import type { JSONAPIResourceObject } from "./resourceObject" import type { JSONAPILinkObject } from "./linkObject"; import type { JSONAPIPaginationLinks } from "./paginationLinks"; import type { JSONAPIResourceIdentifierObject } from "./resourceIdentifierObject"; import type { Satisfies } from "./helpers/satisfies"; +import type { JSONAPIObject } from "./jsonApiObject"; export type JSONAPITopLevelObject = Satisfies<({ data: JSONAPIResourceObject | JSONAPIResourceObject[] | JSONAPIResourceIdentifierObject | JSONAPIResourceIdentifierObject[] | null; @@ -14,15 +15,11 @@ export type JSONAPITopLevelObject = Satisfies<({ } | { errors: JSONAPIErrorObject[]; }) & { - jsonapi?: { - verison?: string; - ext?: string[]; - profile?: string[]; - }, + jsonapi?: JSONAPIObject, meta?: JSONAPIMetaObject; links?: JSONAPILinksObject & { self?: JSONAPILinkObject; related?: JSONAPILinkObject; describedby?: JSONAPILinkObject; } & JSONAPIPaginationLinks; -}, JSONAPIObject>; \ No newline at end of file +}, JSONObject>; \ No newline at end of file