Skip to content

Commit

Permalink
Adding gettable functions for links (#15)
Browse files Browse the repository at this point in the history
* Made tags.list() return an array of TagResource within ListTagsResponse.data

* Added preprocessing check for link strings in client
  • Loading branch information
IAmMadfly authored Sep 27, 2021
1 parent fcfd1e9 commit e2c0e73
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/accounts/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ export interface AccountResource {
export interface ListAccountResponse {
/** The list of accounts returned in this response. */
data: AccountResource[];
links: PaginationLinks;
links: PaginationLinks<ListAccountResponse>;
}
28 changes: 28 additions & 0 deletions src/helper/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ export class UpClient {

public async get<T>(url: string): Promise<T> {
const res = await this.getApi().get<T>(url);

const linksProcessor = (res.data as unknown) as {
links: {
next: null | (() => Promise<T>);
prev: null | (() => Promise<T>);
};
};
if (linksProcessor.links) {
linksProcessor.links.next = linksProcessor.links.next
? async () => {
return (
await this.getApi().get<T>(
(linksProcessor.links.next as unknown) as string
)
).data;
}
: null;
linksProcessor.links.prev = linksProcessor.links.prev
? async () => {
return (
await this.getApi().get<T>(
(linksProcessor.links.prev as unknown) as string
)
).data;
}
: null;
}

return res.data;
}

Expand Down
6 changes: 3 additions & 3 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export interface Relationship<TRelationshipData> {
};
}

export interface PaginationLinks {
export interface PaginationLinks<ReturnType> {
/** The link to the previous page in the results. If this value is null there is no previous page. */
prev: string | null;
prev: () => Promise<ReturnType> | null;
/** The link to the next page in the results. If this value is null there is no next page. */
next: string | null;
next: () => Promise<ReturnType> | null;
}

export interface UpApiError {
Expand Down
15 changes: 2 additions & 13 deletions src/tags/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Relationship } from '../interfaces';
import { Relationship, PaginationLinks } from '../interfaces';

/**
* Provides information about a tag.
Expand Down Expand Up @@ -46,18 +46,7 @@ export interface ListTagsResponse {
* The list of tags returned in this response.
*/
data: TagResource[];
links: {
/**
* The link to the previous page in the results. If this value is `null`
* there is no previous page.
*/
prev: string | null;
/**
* The link to the next page in the results. If this value is `null`
* there is no next page.
*/
next: string | null;
};
links: PaginationLinks<ListTagsResponse>;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/transactions/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,5 @@ export interface TransactionResource {
export interface ListTransactionsResponse {
/** The list of transactions returned in this response. */
data: TransactionResource[];
links: PaginationLinks;
links: PaginationLinks<ListTransactionsResponse>;
}
28 changes: 3 additions & 25 deletions src/webhooks/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Relationship, RelationshipData } from '../interfaces';
import { Relationship, RelationshipData, PaginationLinks } from '../interfaces';

/**
* Provides information about a webhook.
Expand Down Expand Up @@ -68,18 +68,7 @@ export interface ListWebhooksResponse {
* The list of webhooks returned in this response.
*/
data: WebhookResource[];
links: {
/**
* The link to the previous page in the results. If this value is `null`
* there is no previous page.
*/
prev: string | null;
/**
* The link to the next page in the results. If this value is `null`
* there is no next page.
*/
next: string | null;
};
links: PaginationLinks<ListWebhooksResponse>;
}

/**
Expand Down Expand Up @@ -247,16 +236,5 @@ export interface ListWebhookDeliveryLogsResponse {
* The list of delivery logs returned in this response.
*/
data: WebhookDeliveryLogResource[];
links: {
/**
* The link to the previous page in the results. If this value is `null`
* there is no previous page.
*/
prev: string | null;
/**
* The link to the next page in the results. If this value is `null`
* there is no next page.
*/
next: string | null;
};
links: PaginationLinks<ListWebhookDeliveryLogsResponse>;
}

0 comments on commit e2c0e73

Please sign in to comment.