-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added custom relationships endpoint and type
- Loading branch information
George Botzakis
committed
Aug 2, 2024
1 parent
0a1d4a8
commit 77fa4d1
Showing
4 changed files
with
174 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import RequestFactory from '../factories/request' | ||
import { buildURL } from '../utils/helpers' | ||
|
||
class CustomRelationshipsEndpoint { | ||
constructor(endpoint) { | ||
const config = { ...endpoint } | ||
config.version = 'pcm' | ||
this.request = new RequestFactory(config) | ||
this.endpoint = 'custom_relationships' | ||
} | ||
|
||
All() { | ||
const { filter, limit, offset } = this | ||
return this.request.send( | ||
buildURL(this.endpoint, { | ||
filter, | ||
limit, | ||
offset | ||
}), | ||
'GET' | ||
) | ||
} | ||
|
||
Get(slug) { | ||
return this.request.send(`${this.endpoint}/${slug}`, 'GET') | ||
} | ||
|
||
Create(body) { | ||
return this.request.send(this.endpoint, 'POST', { | ||
...body, | ||
type: 'custom-relationship' | ||
}) | ||
} | ||
|
||
Update(slug, body) { | ||
return this.request.send(`${this.endpoint}/${slug}`, 'PUT', { | ||
...body, | ||
type: 'custom-relationship' | ||
}) | ||
} | ||
|
||
Delete(slug) { | ||
return this.request.send(`${this.endpoint}/${slug}`, 'DELETE') | ||
} | ||
|
||
Filter(filter) { | ||
this.filter = filter | ||
return this | ||
} | ||
|
||
Limit(value) { | ||
this.limit = value | ||
return this | ||
} | ||
|
||
Offset(value) { | ||
this.offset = value | ||
return this | ||
} | ||
} | ||
|
||
export default CustomRelationshipsEndpoint |
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,107 @@ | ||
/** | ||
* Custom Relationships | ||
* Description: Custom Relationships | ||
*/ | ||
|
||
import { | ||
Identifiable, | ||
CrudQueryableResource, | ||
ResourcePage, | ||
ResourceList, | ||
Resource | ||
} from './core' | ||
|
||
export interface CustomRelationshipBaseAttributes { | ||
name: string | ||
description?: string | ||
slug: string | ||
} | ||
|
||
export interface CustomRelationshipBase { | ||
type: 'custom-relationship' | ||
attributes: CustomRelationshipBaseAttributes | ||
meta: { | ||
owner: 'organization' | 'store' | ||
timestamps: { | ||
created_at: string | ||
updated_at: string | ||
} | ||
} | ||
} | ||
|
||
export interface CustomRelationship | ||
extends Identifiable, | ||
CustomRelationshipBase {} | ||
|
||
export interface CreateCustomRelationshipBody | ||
extends Pick<CustomRelationshipBase, 'attributes'> {} | ||
|
||
export interface UpdateCustomRelationshipBody | ||
extends Identifiable, | ||
Pick<CustomRelationshipBase, 'attributes'> {} | ||
|
||
export interface CustomRelationshipsFilter { | ||
eq?: { | ||
owner?: 'organization' | 'store' | ||
} | ||
} | ||
|
||
export interface CustomRelationshipsListResponse | ||
extends ResourceList<CustomRelationship> { | ||
links?: { [key: string]: string | null } | {} | ||
meta: { | ||
results: { | ||
total: number | ||
} | ||
} | ||
} | ||
|
||
export interface CustomRelationshipsEndpoint { | ||
endpoint: 'custom_relationships' | ||
/** | ||
* List Custom Relationships | ||
*/ | ||
All(): Promise<CustomRelationshipsListResponse> | ||
|
||
/** | ||
* Get Custom Relationship | ||
* @param slug - The slug of the Custom Relationship. It should always be prefixed with 'CRP_' | ||
*/ | ||
Get(slug: string): Promise<Resource<CustomRelationship>> | ||
|
||
/** | ||
* Create Custom Relationship | ||
* @param body - The base attributes of the Custom Relationships | ||
*/ | ||
Create( | ||
body: CreateCustomRelationshipBody | ||
): Promise<Resource<CustomRelationship>> | ||
|
||
/** | ||
* Update Custom Relationship | ||
* @param slug - The slug of the Custom Relationship. It should always be prefixed with 'CRP_' | ||
* @param body - The base attributes of the Custom Relationships. | ||
* The Slug attribute cannot be updated and the slug within this object should match this function's first argument. | ||
*/ | ||
Update( | ||
slug: string, | ||
body: UpdateCustomRelationshipBody | ||
): Promise<Resource<CustomRelationship>> | ||
|
||
/** | ||
* Delete Custom Relationship | ||
* @param slug - The slug of the Custom Relationship. It should always be prefixed with 'CRP_' | ||
*/ | ||
Delete(slug: string): Promise<{}> | ||
|
||
/** | ||
* Custom Relationship filtering | ||
* @param filter - The filter object. | ||
* Currently supports the 'eq' filter operator for the 'owner' field of the Custom Relationship. | ||
*/ | ||
Filter(filter: CustomRelationshipsFilter): CustomRelationshipsEndpoint | ||
|
||
Limit(value: number): CustomRelationshipsEndpoint | ||
|
||
Offset(value: number): CustomRelationshipsEndpoint | ||
} |