Skip to content

Commit

Permalink
chore(internal): update APIResource structure (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Nov 17, 2023
1 parent 2dae909 commit 3b438f4
Show file tree
Hide file tree
Showing 41 changed files with 167 additions and 189 deletions.
21 changes: 0 additions & 21 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,27 +555,6 @@ export abstract class APIClient {
}
}

export class APIResource {
protected client: APIClient;
constructor(client: APIClient) {
this.client = client;

this.get = client.get.bind(client);
this.post = client.post.bind(client);
this.patch = client.patch.bind(client);
this.put = client.put.bind(client);
this.delete = client.delete.bind(client);
this.getAPIList = client.getAPIList.bind(client);
}

protected get: APIClient['get'];
protected post: APIClient['post'];
protected patch: APIClient['patch'];
protected put: APIClient['put'];
protected delete: APIClient['delete'];
protected getAPIList: APIClient['getAPIList'];
}

export type PageInfo = { url: URL } | { params: Record<string, unknown> | null };

export abstract class AbstractPage<Item> implements AsyncIterable<Item> {
Expand Down
19 changes: 3 additions & 16 deletions src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,9 @@
import type { ModernTreasury } from './index';

export class APIResource {
protected client: ModernTreasury;
constructor(client: ModernTreasury) {
this.client = client;
protected _client: ModernTreasury;

this.get = client.get.bind(client);
this.post = client.post.bind(client);
this.patch = client.patch.bind(client);
this.put = client.put.bind(client);
this.delete = client.delete.bind(client);
this.getAPIList = client.getAPIList.bind(client);
constructor(client: ModernTreasury) {
this._client = client;
}

protected get: ModernTreasury['get'];
protected post: ModernTreasury['post'];
protected patch: ModernTreasury['patch'];
protected put: ModernTreasury['put'];
protected delete: ModernTreasury['delete'];
protected getAPIList: ModernTreasury['getAPIList'];
}
8 changes: 4 additions & 4 deletions src/resources/account-collection-flows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class AccountCollectionFlows extends APIResource {
"The Idempotency-Key request param is deprecated, the 'idempotencyToken' option should be set instead",
);
}
return this.post('/api/account_collection_flows', {
return this._client.post('/api/account_collection_flows', {
body,
...options,
headers: { 'Idempotency-Key': idempotencyKey, ...options?.headers },
Expand All @@ -32,7 +32,7 @@ export class AccountCollectionFlows extends APIResource {
* get account_collection_flow
*/
retrieve(id: string, options?: Core.RequestOptions): Core.APIPromise<AccountCollectionFlow> {
return this.get(`/api/account_collection_flows/${id}`, options);
return this._client.get(`/api/account_collection_flows/${id}`, options);
}

/**
Expand All @@ -50,7 +50,7 @@ export class AccountCollectionFlows extends APIResource {
"The Idempotency-Key request param is deprecated, the 'idempotencyToken' option should be set instead",
);
}
return this.patch(`/api/account_collection_flows/${id}`, {
return this._client.patch(`/api/account_collection_flows/${id}`, {
body,
...options,
headers: { 'Idempotency-Key': idempotencyKey, ...options?.headers },
Expand All @@ -72,7 +72,7 @@ export class AccountCollectionFlows extends APIResource {
if (isRequestOptions(query)) {
return this.list({}, query);
}
return this.getAPIList('/api/account_collection_flows', AccountCollectionFlowsPage, {
return this._client.getAPIList('/api/account_collection_flows', AccountCollectionFlowsPage, {
query,
...options,
});
Expand Down
8 changes: 4 additions & 4 deletions src/resources/account-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class AccountDetails extends APIResource {
"The Idempotency-Key request param is deprecated, the 'idempotencyToken' option should be set instead",
);
}
return this.post(`/api/${accountsType}/${accountId}/account_details`, {
return this._client.post(`/api/${accountsType}/${accountId}/account_details`, {
body,
...options,
headers: { 'Idempotency-Key': idempotencyKey, ...options?.headers },
Expand All @@ -40,7 +40,7 @@ export class AccountDetails extends APIResource {
id: string,
options?: Core.RequestOptions,
): Core.APIPromise<AccountDetail> {
return this.get(`/api/${accountsType}/${accountId}/account_details/${id}`, options);
return this._client.get(`/api/${accountsType}/${accountId}/account_details/${id}`, options);
}

/**
Expand All @@ -66,7 +66,7 @@ export class AccountDetails extends APIResource {
if (isRequestOptions(query)) {
return this.list(accountsType, accountId, {}, query);
}
return this.getAPIList(`/api/${accountsType}/${accountId}/account_details`, AccountDetailsPage, {
return this._client.getAPIList(`/api/${accountsType}/${accountId}/account_details`, AccountDetailsPage, {
query,
...options,
});
Expand All @@ -81,7 +81,7 @@ export class AccountDetails extends APIResource {
id: string,
options?: Core.RequestOptions,
): Core.APIPromise<void> {
return this.delete(`/api/${accountsType}/${accountId}/account_details/${id}`, {
return this._client.delete(`/api/${accountsType}/${accountId}/account_details/${id}`, {
...options,
headers: { Accept: '', ...options?.headers },
});
Expand Down
6 changes: 3 additions & 3 deletions src/resources/bulk-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class BulkRequests extends APIResource {
"The Idempotency-Key request param is deprecated, the 'idempotencyToken' option should be set instead",
);
}
return this.post('/api/bulk_requests', {
return this._client.post('/api/bulk_requests', {
body,
...options,
headers: { 'Idempotency-Key': idempotencyKey, ...options?.headers },
Expand All @@ -34,7 +34,7 @@ export class BulkRequests extends APIResource {
* get bulk_request
*/
retrieve(id: string, options?: Core.RequestOptions): Core.APIPromise<BulkRequest> {
return this.get(`/api/bulk_requests/${id}`, options);
return this._client.get(`/api/bulk_requests/${id}`, options);
}

/**
Expand All @@ -52,7 +52,7 @@ export class BulkRequests extends APIResource {
if (isRequestOptions(query)) {
return this.list({}, query);
}
return this.getAPIList('/api/bulk_requests', BulkRequestsPage, { query, ...options });
return this._client.getAPIList('/api/bulk_requests', BulkRequestsPage, { query, ...options });
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/resources/bulk-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class BulkResults extends APIResource {
* get bulk_result
*/
retrieve(id: string, options?: Core.RequestOptions): Core.APIPromise<BulkResult> {
return this.get(`/api/bulk_results/${id}`, options);
return this._client.get(`/api/bulk_results/${id}`, options);
}

/**
Expand All @@ -32,7 +32,7 @@ export class BulkResults extends APIResource {
if (isRequestOptions(query)) {
return this.list({}, query);
}
return this.getAPIList('/api/bulk_results', BulkResultsPage, { query, ...options });
return this._client.getAPIList('/api/bulk_results', BulkResultsPage, { query, ...options });
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/resources/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Connections extends APIResource {
if (isRequestOptions(query)) {
return this.list({}, query);
}
return this.getAPIList('/api/connections', ConnectionsPage, { query, ...options });
return this._client.getAPIList('/api/connections', ConnectionsPage, { query, ...options });
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/resources/counterparties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Counterparties extends APIResource {
"The Idempotency-Key request param is deprecated, the 'idempotencyToken' option should be set instead",
);
}
return this.post('/api/counterparties', {
return this._client.post('/api/counterparties', {
body,
...options,
headers: { 'Idempotency-Key': idempotencyKey, ...options?.headers },
Expand All @@ -33,7 +33,7 @@ export class Counterparties extends APIResource {
* Get details on a single counterparty.
*/
retrieve(id: string, options?: Core.RequestOptions): Core.APIPromise<Counterparty> {
return this.get(`/api/counterparties/${id}`, options);
return this._client.get(`/api/counterparties/${id}`, options);
}

/**
Expand All @@ -53,7 +53,7 @@ export class Counterparties extends APIResource {
if (isRequestOptions(body)) {
return this.update(id, {}, body);
}
return this.patch(`/api/counterparties/${id}`, { body, ...options });
return this._client.patch(`/api/counterparties/${id}`, { body, ...options });
}

/**
Expand All @@ -71,14 +71,14 @@ export class Counterparties extends APIResource {
if (isRequestOptions(query)) {
return this.list({}, query);
}
return this.getAPIList('/api/counterparties', CounterpartiesPage, { query, ...options });
return this._client.getAPIList('/api/counterparties', CounterpartiesPage, { query, ...options });
}

/**
* Deletes a given counterparty.
*/
del(id: string, options?: Core.RequestOptions): Core.APIPromise<void> {
return this.delete(`/api/counterparties/${id}`, {
return this._client.delete(`/api/counterparties/${id}`, {
...options,
headers: { Accept: '', ...options?.headers },
});
Expand All @@ -99,7 +99,7 @@ export class Counterparties extends APIResource {
"The Idempotency-Key request param is deprecated, the 'idempotencyToken' option should be set instead",
);
}
return this.post(`/api/counterparties/${id}/collect_account`, {
return this._client.post(`/api/counterparties/${id}/collect_account`, {
body,
...options,
headers: { 'Idempotency-Key': idempotencyKey, ...options?.headers },
Expand Down
6 changes: 3 additions & 3 deletions src/resources/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Documents extends APIResource {
"The Idempotency-Key request param is deprecated, the 'idempotencyToken' option should be set instead",
);
}
return this.post(
return this._client.post(
'/api/documents',
multipartFormRequestOptions({
body,
Expand All @@ -33,7 +33,7 @@ export class Documents extends APIResource {
* Get an existing document.
*/
retrieve(id: string, options?: Core.RequestOptions): Core.APIPromise<Document> {
return this.get(`/api/documents/${id}`, options);
return this._client.get(`/api/documents/${id}`, options);
}

/**
Expand All @@ -48,7 +48,7 @@ export class Documents extends APIResource {
if (isRequestOptions(query)) {
return this.list({}, query);
}
return this.getAPIList('/api/documents', DocumentsPage, { query, ...options });
return this._client.getAPIList('/api/documents', DocumentsPage, { query, ...options });
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/resources/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Events extends APIResource {
* get event
*/
retrieve(id: string, options?: Core.RequestOptions): Core.APIPromise<Event> {
return this.get(`/api/events/${id}`, options);
return this._client.get(`/api/events/${id}`, options);
}

/**
Expand All @@ -26,7 +26,7 @@ export class Events extends APIResource {
if (isRequestOptions(query)) {
return this.list({}, query);
}
return this.getAPIList('/api/events', EventsPage, { query, ...options });
return this._client.getAPIList('/api/events', EventsPage, { query, ...options });
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/resources/expected-payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class ExpectedPayments extends APIResource {
"The Idempotency-Key request param is deprecated, the 'idempotencyToken' option should be set instead",
);
}
return this.post('/api/expected_payments', {
return this._client.post('/api/expected_payments', {
body,
...options,
headers: { 'Idempotency-Key': idempotencyKey, ...options?.headers },
Expand All @@ -33,7 +33,7 @@ export class ExpectedPayments extends APIResource {
* get expected payment
*/
retrieve(id: string, options?: Core.RequestOptions): Core.APIPromise<ExpectedPayment> {
return this.get(`/api/expected_payments/${id}`, options);
return this._client.get(`/api/expected_payments/${id}`, options);
}

/**
Expand All @@ -53,7 +53,7 @@ export class ExpectedPayments extends APIResource {
if (isRequestOptions(body)) {
return this.update(id, {}, body);
}
return this.patch(`/api/expected_payments/${id}`, { body, ...options });
return this._client.patch(`/api/expected_payments/${id}`, { body, ...options });
}

/**
Expand All @@ -71,14 +71,14 @@ export class ExpectedPayments extends APIResource {
if (isRequestOptions(query)) {
return this.list({}, query);
}
return this.getAPIList('/api/expected_payments', ExpectedPaymentsPage, { query, ...options });
return this._client.getAPIList('/api/expected_payments', ExpectedPaymentsPage, { query, ...options });
}

/**
* delete expected payment
*/
del(id: string, options?: Core.RequestOptions): Core.APIPromise<ExpectedPayment> {
return this.delete(`/api/expected_payments/${id}`, options);
return this._client.delete(`/api/expected_payments/${id}`, options);
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/resources/external-accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ExternalAccounts extends APIResource {
"The Idempotency-Key request param is deprecated, the 'idempotencyToken' option should be set instead",
);
}
return this.post('/api/external_accounts', {
return this._client.post('/api/external_accounts', {
body,
...options,
headers: { 'Idempotency-Key': idempotencyKey, ...options?.headers },
Expand All @@ -35,7 +35,7 @@ export class ExternalAccounts extends APIResource {
* show external account
*/
retrieve(id: string, options?: Core.RequestOptions): Core.APIPromise<ExternalAccount> {
return this.get(`/api/external_accounts/${id}`, options);
return this._client.get(`/api/external_accounts/${id}`, options);
}

/**
Expand All @@ -55,7 +55,7 @@ export class ExternalAccounts extends APIResource {
if (isRequestOptions(body)) {
return this.update(id, {}, body);
}
return this.patch(`/api/external_accounts/${id}`, { body, ...options });
return this._client.patch(`/api/external_accounts/${id}`, { body, ...options });
}

/**
Expand All @@ -73,14 +73,14 @@ export class ExternalAccounts extends APIResource {
if (isRequestOptions(query)) {
return this.list({}, query);
}
return this.getAPIList('/api/external_accounts', ExternalAccountsPage, { query, ...options });
return this._client.getAPIList('/api/external_accounts', ExternalAccountsPage, { query, ...options });
}

/**
* delete external account
*/
del(id: string, options?: Core.RequestOptions): Core.APIPromise<void> {
return this.delete(`/api/external_accounts/${id}`, {
return this._client.delete(`/api/external_accounts/${id}`, {
...options,
headers: { Accept: '', ...options?.headers },
});
Expand Down Expand Up @@ -110,7 +110,7 @@ export class ExternalAccounts extends APIResource {
"The Idempotency-Key request param is deprecated, the 'idempotencyToken' option should be set instead",
);
}
return this.post(`/api/external_accounts/${id}/complete_verification`, {
return this._client.post(`/api/external_accounts/${id}/complete_verification`, {
body,
...options,
headers: { 'Idempotency-Key': idempotencyKey, ...options?.headers },
Expand All @@ -132,7 +132,7 @@ export class ExternalAccounts extends APIResource {
"The Idempotency-Key request param is deprecated, the 'idempotencyToken' option should be set instead",
);
}
return this.post(`/api/external_accounts/${id}/verify`, {
return this._client.post(`/api/external_accounts/${id}/verify`, {
body,
...options,
headers: { 'Idempotency-Key': idempotencyKey, ...options?.headers },
Expand Down
Loading

0 comments on commit 3b438f4

Please sign in to comment.