From 98fd09955edbc707e732a09387fde9fcc60c1e1c Mon Sep 17 00:00:00 2001 From: fern-api <115122769+fern-api[bot]@users.noreply.github.com> Date: Fri, 26 Jul 2024 13:32:47 +0000 Subject: [PATCH] SDK regeneration --- reference.md | 8 +- src/api/resources/daFs/client/Client.ts | 166 +++++++------- .../eventSubscriptions/client/Client.ts | 208 +++++++++-------- src/api/resources/events/client/Client.ts | 213 +++++++++--------- src/api/resources/grants/client/Client.ts | 208 ++++++++--------- src/core/index.ts | 2 + src/core/pagination/Page.ts | 67 ++++++ src/core/pagination/Pageable.ts | 16 ++ src/core/pagination/index.ts | 2 + src/core/utils/index.ts | 1 + src/core/utils/setObjectProperty.ts | 43 ++++ 11 files changed, 545 insertions(+), 389 deletions(-) create mode 100644 src/core/pagination/Page.ts create mode 100644 src/core/pagination/Pageable.ts create mode 100644 src/core/pagination/index.ts create mode 100644 src/core/utils/index.ts create mode 100644 src/core/utils/setObjectProperty.ts diff --git a/reference.md b/reference.md index adfa6eb..07899d2 100644 --- a/reference.md +++ b/reference.md @@ -341,7 +341,7 @@ await client.connects.get("live_xJd0lUrvpDkzeGBWZbuI2wbvEdM"); ## Grants -
client.grants.list({ ...params }) -> Chariot.GrantsListResponse +
client.grants.list({ ...params }) -> core.Page
@@ -819,7 +819,7 @@ await client.grants.getRecurringGrant("10229488-08d2-4629-b70c-a2f4f4d25344"); ## DaFs -
client.daFs.list({ ...params }) -> Chariot.DaFsListResponse +
client.daFs.list({ ...params }) -> core.Page
@@ -949,7 +949,7 @@ await client.daFs.get("f9e28217-e0f7-4a54-9764-d664ffb10722"); ## Events -
client.events.list({ ...params }) -> Chariot.EventsListResponse +
client.events.list({ ...params }) -> core.Page
@@ -1077,7 +1077,7 @@ await client.events.get("id"); ## EventSubscriptions -
client.eventSubscriptions.list({ ...params }) -> Chariot.EventSubscriptionsListResponse +
client.eventSubscriptions.list({ ...params }) -> core.Page
diff --git a/src/api/resources/daFs/client/Client.ts b/src/api/resources/daFs/client/Client.ts index 7c5c187..bdac40a 100644 --- a/src/api/resources/daFs/client/Client.ts +++ b/src/api/resources/daFs/client/Client.ts @@ -44,91 +44,97 @@ export class DaFs { public async list( request: Chariot.DaFsListRequest = {}, requestOptions?: DaFs.RequestOptions - ): Promise { - const { pageLimit, pageToken } = request; - const _queryParams: Record = {}; - if (pageLimit != null) { - _queryParams["pageLimit"] = pageLimit.toString(); - } - - if (pageToken != null) { - _queryParams["pageToken"] = pageToken; - } - - const _response = await (this._options.fetcher ?? core.fetcher)({ - url: urlJoin( - (await core.Supplier.get(this._options.environment)) ?? environments.ChariotEnvironment.Sandbox, - "v1/dafs" - ), - method: "GET", - headers: { - Authorization: await this._getAuthorizationHeader(), - "X-Fern-Language": "JavaScript", - "X-Fern-SDK-Name": "chariot", - "X-Fern-SDK-Version": "0.0.1-alpha0", - "X-Fern-Runtime": core.RUNTIME.type, - "X-Fern-Runtime-Version": core.RUNTIME.version, - }, - contentType: "application/json", - queryParameters: _queryParams, - requestType: "json", - timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, - maxRetries: requestOptions?.maxRetries, - abortSignal: requestOptions?.abortSignal, - }); - if (_response.ok) { - return serializers.DaFsListResponse.parseOrThrow(_response.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], + ): Promise> { + const list = async (request: Chariot.DaFsListRequest): Promise => { + const { pageLimit, pageToken } = request; + const _queryParams: Record = {}; + if (pageLimit != null) { + _queryParams["pageLimit"] = pageLimit.toString(); + } + if (pageToken != null) { + _queryParams["pageToken"] = pageToken; + } + const _response = await (this._options.fetcher ?? core.fetcher)({ + url: urlJoin( + (await core.Supplier.get(this._options.environment)) ?? environments.ChariotEnvironment.Sandbox, + "v1/dafs" + ), + method: "GET", + headers: { + Authorization: await this._getAuthorizationHeader(), + "X-Fern-Language": "JavaScript", + "X-Fern-SDK-Name": "chariot", + "X-Fern-SDK-Version": "0.0.1-alpha0", + "X-Fern-Runtime": core.RUNTIME.type, + "X-Fern-Runtime-Version": core.RUNTIME.version, + }, + contentType: "application/json", + queryParameters: _queryParams, + requestType: "json", + timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, + maxRetries: requestOptions?.maxRetries, + abortSignal: requestOptions?.abortSignal, }); - } - - if (_response.error.reason === "status-code") { - switch (_response.error.statusCode) { - case 400: - throw new Chariot.BadRequestError( - serializers.Error_.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], - }) - ); - case 500: - throw new Chariot.InternalServerError( - serializers.Error_.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], - }) - ); - default: + if (_response.ok) { + return serializers.DaFsListResponse.parseOrThrow(_response.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }); + } + if (_response.error.reason === "status-code") { + switch (_response.error.statusCode) { + case 400: + throw new Chariot.BadRequestError( + serializers.Error_.parseOrThrow(_response.error.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }) + ); + case 500: + throw new Chariot.InternalServerError( + serializers.Error_.parseOrThrow(_response.error.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }) + ); + default: + throw new errors.ChariotError({ + statusCode: _response.error.statusCode, + body: _response.error.body, + }); + } + } + switch (_response.error.reason) { + case "non-json": throw new errors.ChariotError({ statusCode: _response.error.statusCode, - body: _response.error.body, + body: _response.error.rawBody, + }); + case "timeout": + throw new errors.ChariotTimeoutError(); + case "unknown": + throw new errors.ChariotError({ + message: _response.error.errorMessage, }); } - } - - switch (_response.error.reason) { - case "non-json": - throw new errors.ChariotError({ - statusCode: _response.error.statusCode, - body: _response.error.rawBody, - }); - case "timeout": - throw new errors.ChariotTimeoutError(); - case "unknown": - throw new errors.ChariotError({ - message: _response.error.errorMessage, - }); - } + }; + return new core.Pageable({ + response: await list(request), + hasNextPage: (response) => response?.nextPageToken != null, + getItems: (response) => response?.results ?? [], + loadPage: (response) => { + return list(core.setObjectProperty(request, "pageToken", response?.nextPageToken)); + }, + }); } /** diff --git a/src/api/resources/eventSubscriptions/client/Client.ts b/src/api/resources/eventSubscriptions/client/Client.ts index b51a426..abf51a4 100644 --- a/src/api/resources/eventSubscriptions/client/Client.ts +++ b/src/api/resources/eventSubscriptions/client/Client.ts @@ -46,111 +46,119 @@ export class EventSubscriptions { public async list( request: Chariot.EventSubscriptionsListRequest = {}, requestOptions?: EventSubscriptions.RequestOptions - ): Promise { - const { limit, cursor } = request; - const _queryParams: Record = {}; - if (limit != null) { - _queryParams["limit"] = limit.toString(); - } - - if (cursor != null) { - _queryParams["cursor"] = cursor; - } - - const _response = await (this._options.fetcher ?? core.fetcher)({ - url: urlJoin( - (await core.Supplier.get(this._options.environment)) ?? environments.ChariotEnvironment.Sandbox, - "v1/event_subscriptions" - ), - method: "GET", - headers: { - Authorization: await this._getAuthorizationHeader(), - "X-Fern-Language": "JavaScript", - "X-Fern-SDK-Name": "chariot", - "X-Fern-SDK-Version": "0.0.1-alpha0", - "X-Fern-Runtime": core.RUNTIME.type, - "X-Fern-Runtime-Version": core.RUNTIME.version, - }, - contentType: "application/json", - queryParameters: _queryParams, - requestType: "json", - timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, - maxRetries: requestOptions?.maxRetries, - abortSignal: requestOptions?.abortSignal, - }); - if (_response.ok) { - return serializers.EventSubscriptionsListResponse.parseOrThrow(_response.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], + ): Promise> { + const list = async ( + request: Chariot.EventSubscriptionsListRequest + ): Promise => { + const { limit, cursor } = request; + const _queryParams: Record = {}; + if (limit != null) { + _queryParams["limit"] = limit.toString(); + } + if (cursor != null) { + _queryParams["cursor"] = cursor; + } + const _response = await (this._options.fetcher ?? core.fetcher)({ + url: urlJoin( + (await core.Supplier.get(this._options.environment)) ?? environments.ChariotEnvironment.Sandbox, + "v1/event_subscriptions" + ), + method: "GET", + headers: { + Authorization: await this._getAuthorizationHeader(), + "X-Fern-Language": "JavaScript", + "X-Fern-SDK-Name": "chariot", + "X-Fern-SDK-Version": "0.0.1-alpha0", + "X-Fern-Runtime": core.RUNTIME.type, + "X-Fern-Runtime-Version": core.RUNTIME.version, + }, + contentType: "application/json", + queryParameters: _queryParams, + requestType: "json", + timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, + maxRetries: requestOptions?.maxRetries, + abortSignal: requestOptions?.abortSignal, }); - } - - if (_response.error.reason === "status-code") { - switch (_response.error.statusCode) { - case 400: - throw new Chariot.BadRequestError( - serializers.Error_.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], - }) - ); - case 401: - throw new Chariot.UnauthorizedError( - serializers.Error_.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], - }) - ); - case 403: - throw new Chariot.ForbiddenError( - serializers.Error_.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], - }) - ); - case 500: - throw new Chariot.InternalServerError( - serializers.Error_.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], - }) - ); - default: + if (_response.ok) { + return serializers.EventSubscriptionsListResponse.parseOrThrow(_response.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }); + } + if (_response.error.reason === "status-code") { + switch (_response.error.statusCode) { + case 400: + throw new Chariot.BadRequestError( + serializers.Error_.parseOrThrow(_response.error.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }) + ); + case 401: + throw new Chariot.UnauthorizedError( + serializers.Error_.parseOrThrow(_response.error.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }) + ); + case 403: + throw new Chariot.ForbiddenError( + serializers.Error_.parseOrThrow(_response.error.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }) + ); + case 500: + throw new Chariot.InternalServerError( + serializers.Error_.parseOrThrow(_response.error.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }) + ); + default: + throw new errors.ChariotError({ + statusCode: _response.error.statusCode, + body: _response.error.body, + }); + } + } + switch (_response.error.reason) { + case "non-json": throw new errors.ChariotError({ statusCode: _response.error.statusCode, - body: _response.error.body, + body: _response.error.rawBody, + }); + case "timeout": + throw new errors.ChariotTimeoutError(); + case "unknown": + throw new errors.ChariotError({ + message: _response.error.errorMessage, }); } - } - - switch (_response.error.reason) { - case "non-json": - throw new errors.ChariotError({ - statusCode: _response.error.statusCode, - body: _response.error.rawBody, - }); - case "timeout": - throw new errors.ChariotTimeoutError(); - case "unknown": - throw new errors.ChariotError({ - message: _response.error.errorMessage, - }); - } + }; + return new core.Pageable({ + response: await list(request), + hasNextPage: (response) => response?.nextPageToken != null, + getItems: (response) => response?.results ?? [], + loadPage: (response) => { + return list(core.setObjectProperty(request, "cursor", response?.nextPageToken)); + }, + }); } /** diff --git a/src/api/resources/events/client/Client.ts b/src/api/resources/events/client/Client.ts index 4d79a66..645d722 100644 --- a/src/api/resources/events/client/Client.ts +++ b/src/api/resources/events/client/Client.ts @@ -46,115 +46,120 @@ export class Events { public async list( request: Chariot.EventsListRequest = {}, requestOptions?: Events.RequestOptions - ): Promise { - const { limit, cursor, category } = request; - const _queryParams: Record = {}; - if (limit != null) { - _queryParams["limit"] = limit.toString(); - } - - if (cursor != null) { - _queryParams["cursor"] = cursor; - } - - if (category != null) { - _queryParams["category"] = category; - } - - const _response = await (this._options.fetcher ?? core.fetcher)({ - url: urlJoin( - (await core.Supplier.get(this._options.environment)) ?? environments.ChariotEnvironment.Sandbox, - "v1/events" - ), - method: "GET", - headers: { - Authorization: await this._getAuthorizationHeader(), - "X-Fern-Language": "JavaScript", - "X-Fern-SDK-Name": "chariot", - "X-Fern-SDK-Version": "0.0.1-alpha0", - "X-Fern-Runtime": core.RUNTIME.type, - "X-Fern-Runtime-Version": core.RUNTIME.version, - }, - contentType: "application/json", - queryParameters: _queryParams, - requestType: "json", - timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, - maxRetries: requestOptions?.maxRetries, - abortSignal: requestOptions?.abortSignal, - }); - if (_response.ok) { - return serializers.EventsListResponse.parseOrThrow(_response.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], + ): Promise> { + const list = async (request: Chariot.EventsListRequest): Promise => { + const { limit, cursor, category } = request; + const _queryParams: Record = {}; + if (limit != null) { + _queryParams["limit"] = limit.toString(); + } + if (cursor != null) { + _queryParams["cursor"] = cursor; + } + if (category != null) { + _queryParams["category"] = category; + } + const _response = await (this._options.fetcher ?? core.fetcher)({ + url: urlJoin( + (await core.Supplier.get(this._options.environment)) ?? environments.ChariotEnvironment.Sandbox, + "v1/events" + ), + method: "GET", + headers: { + Authorization: await this._getAuthorizationHeader(), + "X-Fern-Language": "JavaScript", + "X-Fern-SDK-Name": "chariot", + "X-Fern-SDK-Version": "0.0.1-alpha0", + "X-Fern-Runtime": core.RUNTIME.type, + "X-Fern-Runtime-Version": core.RUNTIME.version, + }, + contentType: "application/json", + queryParameters: _queryParams, + requestType: "json", + timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, + maxRetries: requestOptions?.maxRetries, + abortSignal: requestOptions?.abortSignal, }); - } - - if (_response.error.reason === "status-code") { - switch (_response.error.statusCode) { - case 400: - throw new Chariot.BadRequestError( - serializers.Error_.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], - }) - ); - case 401: - throw new Chariot.UnauthorizedError( - serializers.Error_.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], - }) - ); - case 403: - throw new Chariot.ForbiddenError( - serializers.Error_.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], - }) - ); - case 500: - throw new Chariot.InternalServerError( - serializers.Error_.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], - }) - ); - default: + if (_response.ok) { + return serializers.EventsListResponse.parseOrThrow(_response.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }); + } + if (_response.error.reason === "status-code") { + switch (_response.error.statusCode) { + case 400: + throw new Chariot.BadRequestError( + serializers.Error_.parseOrThrow(_response.error.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }) + ); + case 401: + throw new Chariot.UnauthorizedError( + serializers.Error_.parseOrThrow(_response.error.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }) + ); + case 403: + throw new Chariot.ForbiddenError( + serializers.Error_.parseOrThrow(_response.error.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }) + ); + case 500: + throw new Chariot.InternalServerError( + serializers.Error_.parseOrThrow(_response.error.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }) + ); + default: + throw new errors.ChariotError({ + statusCode: _response.error.statusCode, + body: _response.error.body, + }); + } + } + switch (_response.error.reason) { + case "non-json": throw new errors.ChariotError({ statusCode: _response.error.statusCode, - body: _response.error.body, + body: _response.error.rawBody, + }); + case "timeout": + throw new errors.ChariotTimeoutError(); + case "unknown": + throw new errors.ChariotError({ + message: _response.error.errorMessage, }); } - } - - switch (_response.error.reason) { - case "non-json": - throw new errors.ChariotError({ - statusCode: _response.error.statusCode, - body: _response.error.rawBody, - }); - case "timeout": - throw new errors.ChariotTimeoutError(); - case "unknown": - throw new errors.ChariotError({ - message: _response.error.errorMessage, - }); - } + }; + return new core.Pageable({ + response: await list(request), + hasNextPage: (response) => response?.nextPageToken != null, + getItems: (response) => response?.results ?? [], + loadPage: (response) => { + return list(core.setObjectProperty(request, "cursor", response?.nextPageToken)); + }, + }); } /** diff --git a/src/api/resources/grants/client/Client.ts b/src/api/resources/grants/client/Client.ts index 05b36a5..6b0a522 100644 --- a/src/api/resources/grants/client/Client.ts +++ b/src/api/resources/grants/client/Client.ts @@ -48,112 +48,118 @@ export class Grants { public async list( request: Chariot.GrantsListRequest, requestOptions?: Grants.RequestOptions - ): Promise { - const { pageLimit, pageToken, chariotApiKey } = request; - const _queryParams: Record = {}; - if (pageLimit != null) { - _queryParams["pageLimit"] = pageLimit.toString(); - } - - if (pageToken != null) { - _queryParams["pageToken"] = pageToken; - } - - const _response = await (this._options.fetcher ?? core.fetcher)({ - url: urlJoin( - (await core.Supplier.get(this._options.environment)) ?? environments.ChariotEnvironment.Sandbox, - "v1/unintegrated_grants" - ), - method: "GET", - headers: { - Authorization: await this._getAuthorizationHeader(), - "X-Fern-Language": "JavaScript", - "X-Fern-SDK-Name": "chariot", - "X-Fern-SDK-Version": "0.0.1-alpha0", - "X-Fern-Runtime": core.RUNTIME.type, - "X-Fern-Runtime-Version": core.RUNTIME.version, - "x-chariot-api-key": chariotApiKey, - }, - contentType: "application/json", - queryParameters: _queryParams, - requestType: "json", - timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, - maxRetries: requestOptions?.maxRetries, - abortSignal: requestOptions?.abortSignal, - }); - if (_response.ok) { - return serializers.GrantsListResponse.parseOrThrow(_response.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], + ): Promise> { + const list = async (request: Chariot.GrantsListRequest): Promise => { + const { pageLimit, pageToken, chariotApiKey } = request; + const _queryParams: Record = {}; + if (pageLimit != null) { + _queryParams["pageLimit"] = pageLimit.toString(); + } + if (pageToken != null) { + _queryParams["pageToken"] = pageToken; + } + const _response = await (this._options.fetcher ?? core.fetcher)({ + url: urlJoin( + (await core.Supplier.get(this._options.environment)) ?? environments.ChariotEnvironment.Sandbox, + "v1/unintegrated_grants" + ), + method: "GET", + headers: { + Authorization: await this._getAuthorizationHeader(), + "X-Fern-Language": "JavaScript", + "X-Fern-SDK-Name": "chariot", + "X-Fern-SDK-Version": "0.0.1-alpha0", + "X-Fern-Runtime": core.RUNTIME.type, + "X-Fern-Runtime-Version": core.RUNTIME.version, + "x-chariot-api-key": chariotApiKey, + }, + contentType: "application/json", + queryParameters: _queryParams, + requestType: "json", + timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, + maxRetries: requestOptions?.maxRetries, + abortSignal: requestOptions?.abortSignal, }); - } - - if (_response.error.reason === "status-code") { - switch (_response.error.statusCode) { - case 400: - throw new Chariot.BadRequestError( - serializers.Error_.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], - }) - ); - case 401: - throw new Chariot.UnauthorizedError( - serializers.Error_.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], - }) - ); - case 403: - throw new Chariot.ForbiddenError( - serializers.Error_.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], - }) - ); - case 500: - throw new Chariot.InternalServerError( - serializers.Error_.parseOrThrow(_response.error.body, { - unrecognizedObjectKeys: "passthrough", - allowUnrecognizedUnionMembers: true, - allowUnrecognizedEnumValues: true, - skipValidation: true, - breadcrumbsPrefix: ["response"], - }) - ); - default: + if (_response.ok) { + return serializers.GrantsListResponse.parseOrThrow(_response.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }); + } + if (_response.error.reason === "status-code") { + switch (_response.error.statusCode) { + case 400: + throw new Chariot.BadRequestError( + serializers.Error_.parseOrThrow(_response.error.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }) + ); + case 401: + throw new Chariot.UnauthorizedError( + serializers.Error_.parseOrThrow(_response.error.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }) + ); + case 403: + throw new Chariot.ForbiddenError( + serializers.Error_.parseOrThrow(_response.error.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }) + ); + case 500: + throw new Chariot.InternalServerError( + serializers.Error_.parseOrThrow(_response.error.body, { + unrecognizedObjectKeys: "passthrough", + allowUnrecognizedUnionMembers: true, + allowUnrecognizedEnumValues: true, + skipValidation: true, + breadcrumbsPrefix: ["response"], + }) + ); + default: + throw new errors.ChariotError({ + statusCode: _response.error.statusCode, + body: _response.error.body, + }); + } + } + switch (_response.error.reason) { + case "non-json": throw new errors.ChariotError({ statusCode: _response.error.statusCode, - body: _response.error.body, + body: _response.error.rawBody, + }); + case "timeout": + throw new errors.ChariotTimeoutError(); + case "unknown": + throw new errors.ChariotError({ + message: _response.error.errorMessage, }); } - } - - switch (_response.error.reason) { - case "non-json": - throw new errors.ChariotError({ - statusCode: _response.error.statusCode, - body: _response.error.rawBody, - }); - case "timeout": - throw new errors.ChariotTimeoutError(); - case "unknown": - throw new errors.ChariotError({ - message: _response.error.errorMessage, - }); - } + }; + return new core.Pageable({ + response: await list(request), + hasNextPage: (response) => response?.nextPageToken != null, + getItems: (response) => response?.results ?? [], + loadPage: (response) => { + return list(core.setObjectProperty(request, "pageToken", response?.nextPageToken)); + }, + }); } /** diff --git a/src/core/index.ts b/src/core/index.ts index 2d20c46..2c2bf0c 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -1,4 +1,6 @@ export * from "./fetcher"; export * from "./auth"; export * from "./runtime"; +export * from "./utils"; +export * from "./pagination"; export * as serialization from "./schemas"; diff --git a/src/core/pagination/Page.ts b/src/core/pagination/Page.ts new file mode 100644 index 0000000..07c6796 --- /dev/null +++ b/src/core/pagination/Page.ts @@ -0,0 +1,67 @@ +/** + * A page of results from a paginated API. + * + * @template T The type of the items in the page. + */ +export class Page implements AsyncIterable { + public data: T[]; + + private response: unknown; + private _hasNextPage: (response: unknown) => boolean; + private getItems: (response: unknown) => T[]; + private loadNextPage: (response: unknown) => Promise; + + constructor({ + response, + hasNextPage, + getItems, + loadPage, + }: { + response: unknown; + hasNextPage: (response: unknown) => boolean; + getItems: (response: unknown) => T[]; + loadPage: (response: unknown) => Promise; + }) { + this.response = response; + this.data = getItems(response); + this._hasNextPage = hasNextPage; + this.getItems = getItems; + this.loadNextPage = loadPage; + } + + /** + * Retrieves the next page + * @returns this + */ + public async getNextPage(): Promise { + this.response = await this.loadNextPage(this.response); + this.data = this.getItems(this.response); + return this; + } + + /** + * @returns whether there is a next page to load + */ + public hasNextPage(): boolean { + return this._hasNextPage(this.response); + } + + private async *iterMessages(): AsyncGenerator { + for (const item of this.data) { + yield item; + } + + while (this.hasNextPage()) { + await this.getNextPage(); + for (const item of this.data) { + yield item; + } + } + } + + async *[Symbol.asyncIterator](): AsyncIterator { + for await (const message of this.iterMessages()) { + yield message; + } + } +} diff --git a/src/core/pagination/Pageable.ts b/src/core/pagination/Pageable.ts new file mode 100644 index 0000000..befce63 --- /dev/null +++ b/src/core/pagination/Pageable.ts @@ -0,0 +1,16 @@ +import { Page } from "./Page"; + +export declare namespace Pageable { + interface Args { + response: Response; + hasNextPage: (response: Response) => boolean; + getItems: (response: Response) => Item[]; + loadPage: (response: Response) => Promise; + } +} + +export class Pageable extends Page { + constructor(args: Pageable.Args) { + super(args as any); + } +} diff --git a/src/core/pagination/index.ts b/src/core/pagination/index.ts new file mode 100644 index 0000000..2ceb268 --- /dev/null +++ b/src/core/pagination/index.ts @@ -0,0 +1,2 @@ +export { Page } from "./Page"; +export { Pageable } from "./Pageable"; diff --git a/src/core/utils/index.ts b/src/core/utils/index.ts new file mode 100644 index 0000000..b168f59 --- /dev/null +++ b/src/core/utils/index.ts @@ -0,0 +1 @@ +export { setObjectProperty } from "./setObjectProperty"; diff --git a/src/core/utils/setObjectProperty.ts b/src/core/utils/setObjectProperty.ts new file mode 100644 index 0000000..5528af2 --- /dev/null +++ b/src/core/utils/setObjectProperty.ts @@ -0,0 +1,43 @@ +/** + * Sets the value at path of object. If a portion of path doesn’t exist it’s created. This is + * inspired by Lodash's set function, but is simplified to accommodate our use case. + * For more details, see https://lodash.com/docs/4.17.15#set. + * + * @param object The object to modify. + * @param path The path of the property to set. + * @param value The value to set. + * @return Returns object. + */ +export function setObjectProperty(object: T, path: string, value: any): T { + if (object == null) { + return object; + } + + const keys: string[] = path.split("."); + if (keys.length === 0) { + // Invalid path; do nothing. + return object; + } + + let current: Record = object; + for (let i = 0; i < keys.length - 1; i++) { + const key = keys[i]; + if (key == null) { + // Unreachable. + continue; + } + if (!current[key] || typeof current[key] !== "object") { + current[key] = {}; + } + current = current[key] as Record; + } + + const lastKey = keys[keys.length - 1]; + if (lastKey == null) { + // Unreachable. + return object; + } + + current[lastKey] = value; + return object; +}