-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add JSON:API helpers * Update tests to use JSON:API helpers * Fix paths to SDK in tests * Fix variable name and tests * Add findSingleRelationshipDocument
- Loading branch information
Showing
14 changed files
with
125 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import SpreeSDKError from './SpreeSDKError' | ||
|
||
export default class DocumentRelationshipError extends SpreeSDKError { | ||
constructor(message: string) { | ||
super(message) | ||
Object.setPrototypeOf(this, DocumentRelationshipError.prototype) | ||
this.name = 'DocumentRelationshipError' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { JsonApiDocument, JsonApiResponse } from '../interfaces/JsonApi' | ||
import type { RelationType } from '../interfaces/Relationships' | ||
import { DocumentRelationshipError } from '../errors' | ||
|
||
const findDocument = <DocumentType extends JsonApiDocument>( | ||
spreeSuccessResponse: JsonApiResponse, | ||
relationType: RelationType | ||
): DocumentType | null => { | ||
if (!spreeSuccessResponse.included) { | ||
return null | ||
} | ||
|
||
return ( | ||
(spreeSuccessResponse.included.find( | ||
(includedObject) => includedObject.type === relationType.type && includedObject.id === relationType.id | ||
) as DocumentType) || null | ||
) | ||
} | ||
|
||
const findRelationshipDocuments = <DocumentType extends JsonApiDocument>( | ||
spreeSuccessResponse: JsonApiResponse, | ||
sourceDocument: JsonApiDocument, | ||
relationshipName: string | ||
): DocumentType[] => { | ||
if (!spreeSuccessResponse.included) { | ||
return [] | ||
} | ||
|
||
const oneOrManyDocumentReferences = sourceDocument.relationships[relationshipName]?.data | ||
let documentReferences: RelationType[] | ||
|
||
if (!oneOrManyDocumentReferences) { | ||
throw new DocumentRelationshipError(`Incorrect relationship ${relationshipName}.`) | ||
} | ||
|
||
if (Array.isArray(oneOrManyDocumentReferences)) { | ||
documentReferences = oneOrManyDocumentReferences | ||
} else { | ||
documentReferences = [oneOrManyDocumentReferences] | ||
} | ||
|
||
return documentReferences | ||
.map<DocumentType>((relationType: RelationType) => findDocument<DocumentType>(spreeSuccessResponse, relationType)) | ||
.filter(Boolean) | ||
} | ||
|
||
const findSingleRelationshipDocument = <DocumentType extends JsonApiDocument>( | ||
spreeSuccessResponse: JsonApiResponse, | ||
sourceDocument: JsonApiDocument, | ||
relationshipName: string | ||
): DocumentType | null => { | ||
const documents = findRelationshipDocuments<DocumentType>(spreeSuccessResponse, sourceDocument, relationshipName) | ||
|
||
if (documents.length === 0) { | ||
return null | ||
} | ||
|
||
return documents[0] | ||
} | ||
|
||
export { findDocument, findRelationshipDocuments, findSingleRelationshipDocument } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import Client from './Client' | ||
import * as errors from './errors' | ||
import * as result from './helpers/result' | ||
import * as jsonApi from './helpers/jsonApi' | ||
import Http from './Http' | ||
import routes, { storefrontPath } from './routes' | ||
import * as endpoints from './endpoints' | ||
import makeClient from './makeClient' | ||
|
||
export { Client, Http, result, errors, makeClient, endpoints, routes, storefrontPath } | ||
export { Client, Http, result, errors, makeClient, endpoints, routes, storefrontPath, jsonApi } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"rules": { | ||
"@typescript-eslint/no-non-null-assertion": "off", | ||
"max-lines-per-function": "off" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import SpreeSDKError from './SpreeSDKError'; | ||
export default class CastError extends SpreeSDKError { | ||
constructor(name: string); | ||
constructor(message: string); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import SpreeSDKError from './SpreeSDKError'; | ||
export default class DeserializeError extends SpreeSDKError { | ||
constructor(name: string); | ||
constructor(message: string); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import SpreeSDKError from './SpreeSDKError'; | ||
export default class DocumentRelationshipError extends SpreeSDKError { | ||
constructor(message: string); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { JsonApiDocument, JsonApiResponse } from '../interfaces/JsonApi'; | ||
import type { RelationType } from '../interfaces/Relationships'; | ||
declare const findDocument: <DocumentType_1 extends JsonApiDocument>(spreeSuccessResponse: JsonApiResponse, relationType: RelationType) => DocumentType_1; | ||
declare const findRelationshipDocuments: <DocumentType_1 extends JsonApiDocument>(spreeSuccessResponse: JsonApiResponse, sourceDocument: JsonApiDocument, relationshipName: string) => DocumentType_1[]; | ||
declare const findSingleRelationshipDocument: <DocumentType_1 extends JsonApiDocument>(spreeSuccessResponse: JsonApiResponse, sourceDocument: JsonApiDocument, relationshipName: string) => DocumentType_1; | ||
export { findDocument, findRelationshipDocuments, findSingleRelationshipDocument }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import Client from './Client'; | ||
import * as errors from './errors'; | ||
import * as result from './helpers/result'; | ||
import * as jsonApi from './helpers/jsonApi'; | ||
import Http from './Http'; | ||
import routes, { storefrontPath } from './routes'; | ||
import * as endpoints from './endpoints'; | ||
import makeClient from './makeClient'; | ||
export { Client, Http, result, errors, makeClient, endpoints, routes, storefrontPath }; | ||
export { Client, Http, result, errors, makeClient, endpoints, routes, storefrontPath, jsonApi }; |