Skip to content

Commit

Permalink
Extract common relationship object and fix incorrect/missing relation…
Browse files Browse the repository at this point in the history
…ships (#13)

- Updated Relationship to take a type for the 'data' key, and added new RelationshipData type which takes the 'type' value (typically a string)
  - This will make relationship data types more strict and reflect the API
- Fixed Account translations relationship
- Added Transaction transferAccount relationship (added to API as part of up-banking/api#66)
  • Loading branch information
daveallie authored Apr 6, 2021
1 parent 4cc278e commit e1aabdd
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 113 deletions.
7 changes: 2 additions & 5 deletions src/accounts/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MoneyObject, PaginationLinks } from '../interfaces';
import { MoneyObject, PaginationLinks, Relationship } from '../interfaces';

export interface ListAccountsRequest {
/** The number of records to return in each page. e.g. ?page[size]=30 */
Expand Down Expand Up @@ -26,10 +26,7 @@ export interface AccountResource {
createdAt: string;
};
relationships: {
links?: {
/** The link to retrieve the related resource(s) in this relationship. */
related: string;
};
transactions: Relationship<undefined>;
};
links: {
/** The canonical link to this resource within the API. */
Expand Down
18 changes: 3 additions & 15 deletions src/categories/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Relationship } from '../interfaces';
import { Relationship, RelationshipData } from '../interfaces';

export interface ListCategoriesRequest {
/**
Expand All @@ -19,20 +19,8 @@ export interface CategoryResource {
name: string;
};
relationships: {
parent: {
data: null | Relationship;
links?: {
/** The link to retrieve the related resource(s) in this relationship. */
related: string;
};
};
children: {
data: Relationship[];
links?: {
/** The link to retrieve the related resource(s) in this relationship. */
related: string;
};
};
parent: Relationship<null | RelationshipData<'categories'>>;
children: Relationship<Array<RelationshipData<'categories'>>>;
};
links: {
/** The canonical link to this resource within the API. */
Expand Down
12 changes: 10 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ export interface MoneyObject {
valueInBaseUnits: number;
}

export interface Relationship {
export interface RelationshipData<RelationshipType extends string> {
/** The type of this resource */
type: string;
type: RelationshipType;
/** The unique identifier of the resource within its type. */
id: string;
}

export interface Relationship<TRelationshipData> {
data: TRelationshipData;
links?: {
/** The link to retrieve the related resource(s) in this relationship. */
related: string;
};
}

export interface PaginationLinks {
/** The link to the previous page in the results. If this value is null there is no previous page. */
prev: string | null;
Expand Down
11 changes: 3 additions & 8 deletions src/tags/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Relationship } from '../interfaces';

/**
* Provides information about a tag.
*/
Expand All @@ -11,14 +13,7 @@ export interface TagResource {
*/
id: string;
relationships: {
transactions: {
links?: {
/**
* The link to retrieve the related resource(s) in this relationship.
*/
related: string;
};
};
transactions: Relationship<undefined>;
};
}

Expand Down
40 changes: 11 additions & 29 deletions src/transactions/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { MoneyObject, PaginationLinks, Relationship } from '../interfaces';
import {
MoneyObject,
PaginationLinks,
Relationship,
RelationshipData,
} from '../interfaces';

export interface ListTransactionRequest {
/** The number of records to return in each page. e.g. ?page[size]=30 */
Expand Down Expand Up @@ -116,34 +121,11 @@ export interface TransactionResource {
createdAt: string;
};
relationships: {
account: {
data: Relationship;
links?: {
/** The link to retrieve the related resource(s) in this relationship. */
related: string;
};
};
category: {
data: null | Relationship;
links?: {
/** The link to retrieve the related resource(s) in this relationship. */
related: string;
};
};
parentCategory: {
data: null | Relationship;
links?: {
/** The link to retrieve the related resource(s) in this relationship. */
related: string;
};
};
tags: {
data: Relationship[];
links?: {
/** The link to retrieve the related resource(s) in this relationship. */
related: string;
};
};
account: Relationship<RelationshipData<'accounts'>>;
transferAccount: Relationship<null | RelationshipData<'accounts'>>;
category: Relationship<null | RelationshipData<'categories'>>;
parentCategory: Relationship<null | RelationshipData<'categories'>>;
tags: Relationship<Array<RelationshipData<'tags'>>>;
};
links: {
/** The canonical link to this resource within the API. */
Expand Down
60 changes: 6 additions & 54 deletions src/webhooks/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Relationship, RelationshipData } from '../interfaces';

/**
* Provides information about a webhook.
*/
Expand Down Expand Up @@ -41,14 +43,7 @@ export interface WebhookResource {
createdAt: string;
};
relationships: {
logs: {
links?: {
/**
* The link to retrieve the related resource(s) in this relationship.
*/
related: string;
};
};
logs: Relationship<undefined>;
};
links?: {
/**
Expand Down Expand Up @@ -157,42 +152,8 @@ export interface WebhookEventResource {
createdAt: string;
};
relationships: {
webhook: {
data: {
/**
* The type of this resource: `webhooks`
*/
type: string;
/**
* The unique identifier of the resource within its type.
*/
id: string;
};
links?: {
/**
* The link to retrieve the related resource(s) in this relationship.
*/
related: string;
};
};
transaction?: {
data: {
/**
* The type of this resource: `transactions`
*/
type: string;
/**
* The unique identifier of the resource within its type.
*/
id: string;
};
links?: {
/**
* The link to retrieve the related resource(s) in this relationship.
*/
related: string;
};
};
webhook: Relationship<RelationshipData<'webhooks'>>;
transaction?: Relationship<RelationshipData<'transactions'>>;
};
}

Expand Down Expand Up @@ -271,16 +232,7 @@ export interface WebhookDeliveryLogResource {
};
relationships: {
webhookEvent: {
data: {
/**
* The type of this resource: `webhook-events`
*/
type: string;
/**
* The unique identifier of the resource within its type.
*/
id: string;
};
data: RelationshipData<'webhook-events'>;
};
};
}
Expand Down

0 comments on commit e1aabdd

Please sign in to comment.