diff --git a/examples/nodejs/cache/delete-store.ts b/examples/nodejs/cache/delete-store.ts new file mode 100644 index 000000000..82cec7b5a --- /dev/null +++ b/examples/nodejs/cache/delete-store.ts @@ -0,0 +1,46 @@ +import { + CredentialProvider, + DeleteStoreResponse, + ListStoresResponse, + PreviewStorageClient, + StorageConfigurations, +} from '@gomomento/sdk'; + +async function main() { + const storageClient = new PreviewStorageClient({ + configuration: StorageConfigurations.Laptop.latest(), + credentialProvider: CredentialProvider.fromEnvironmentVariable({ + environmentVariableName: 'MOMENTO_API_KEY', + }), + }); + + // list stores + const listStoresResponse = await storageClient.listStores(); + + switch (listStoresResponse.type) { + case ListStoresResponse.Success: { + const storeNames = listStoresResponse.stores().map(store => store.getName()); + for (const store of storeNames) { + const deleteStoreResponse = await storageClient.deleteStore(store); + if (deleteStoreResponse.type === DeleteStoreResponse.Success) { + console.log(`Deleted store ${store}`); + } else { + console.error(`Error deleting store ${store}: ${deleteStoreResponse.message()}`); + } + } + break; + } + case ListStoresResponse.Error: { + console.error(`Error listing stores: ${listStoresResponse.message()}`); + } + } +} + +main() + .then(() => { + console.log('success!!'); + }) + .catch((e: Error) => { + console.error(`Uncaught exception while running example: ${e.message}`); + throw e; + }); diff --git a/examples/nodejs/cache/package-lock.json b/examples/nodejs/cache/package-lock.json index e4cace0c3..873086b18 100644 --- a/examples/nodejs/cache/package-lock.json +++ b/examples/nodejs/cache/package-lock.json @@ -122,6 +122,7 @@ "version": "0.9.5", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.9.5.tgz", "integrity": "sha512-ObyMyWxZiCu/yTisA7uzx81s40xR2fD5Cg/2Kq7G02ajkNubJf6BopgDTmDyc3U7sXpNKM8cYOw7s7Tyr+DnCw==", + "deprecated": "Use @eslint/config-array instead", "dev": true, "dependencies": { "@humanwhocodes/object-schema": "^1.2.1", @@ -136,6 +137,7 @@ "version": "1.2.1", "resolved": "https://registry.npmjs.org/@humanwhocodes/object-schema/-/object-schema-1.2.1.tgz", "integrity": "sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==", + "deprecated": "Use @eslint/object-schema instead", "dev": true }, "node_modules/@js-sdsl/ordered-map": { @@ -584,9 +586,9 @@ "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==" }, "node_modules/acorn": { - "version": "8.11.3", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz", - "integrity": "sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==", + "version": "8.12.1", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.12.1.tgz", + "integrity": "sha512-tcpGyI9zbizT9JbV6oYE477V6mTlXvvi0T0G3SNIYE2apm/G5huBa1+K89VGeovbg+jycCrfhl3ADxErOuO6Jg==", "dev": true, "bin": { "acorn": "bin/acorn" @@ -1010,9 +1012,9 @@ } }, "node_modules/debug": { - "version": "4.3.4", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", - "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "version": "4.3.6", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.6.tgz", + "integrity": "sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==", "dependencies": { "ms": "2.1.2" }, @@ -1636,9 +1638,9 @@ } }, "node_modules/esquery": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/esquery/-/esquery-1.5.0.tgz", - "integrity": "sha512-YQLXUplAwJgCydQ78IMJywZCceoqk1oH01OERdSAJc/7U2AylwjhSCLDEtqwg811idIS/9fIU5GjG73IgjKMVg==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/esquery/-/esquery-1.6.0.tgz", + "integrity": "sha512-ca9pw9fomFcKPvFLXhBKUK90ZvGibiGOvRJNbjljY7s7uq/5YO4BOzcYtJqExdx99rF6aAcnRxHmcUHcz6sQsg==", "dev": true, "dependencies": { "estraverse": "^5.1.0" @@ -2182,9 +2184,9 @@ ] }, "node_modules/ignore": { - "version": "5.3.1", - "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.1.tgz", - "integrity": "sha512-5Fytz/IraMjqpwfd34ke28PTVMjZjJG2MPn5t7OE4eUCUNf8BAa7b5WUS9/Qvr6mwOQS7Mk6vdsMno5he+T8Xw==", + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz", + "integrity": "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==", "dev": true, "engines": { "node": ">= 4" @@ -2301,12 +2303,15 @@ } }, "node_modules/is-core-module": { - "version": "2.13.1", - "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.13.1.tgz", - "integrity": "sha512-hHrIjvZsftOsvKSn2TRYl63zvxsgE0K+0mYMoH6gD4omR5IWB2KynivBQczo3+wF1cCkjzvptnI9Q0sPU66ilw==", + "version": "2.15.0", + "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.15.0.tgz", + "integrity": "sha512-Dd+Lb2/zvk9SKy1TGCt1wFJFo/MWBPMX5x7KcvLajWTGuomczdQX61PvY5yK6SVACwpoexWo81IfFyoKY2QnTA==", "dev": true, "dependencies": { - "hasown": "^2.0.0" + "hasown": "^2.0.2" + }, + "engines": { + "node": ">= 0.4" }, "funding": { "url": "https://github.com/sponsors/ljharb" @@ -2763,10 +2768,13 @@ } }, "node_modules/object-inspect": { - "version": "1.13.1", - "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.1.tgz", - "integrity": "sha512-5qoj1RUiKOMsCCNLV1CBiPYE10sziTsnmNxkAI/rZhiD63CF7IqdFGC/XzjWjpSgLf0LxXX3bDFIh0E18f6UhQ==", + "version": "1.13.2", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.2.tgz", + "integrity": "sha512-IRZSRuzJiynemAXPYtPe5BoI/RESNYR7TYm50MC5Mqbd3Jmw5y790sErYw3V6SryFJD64b74qQQs9wn5Bg/k3g==", "dev": true, + "engines": { + "node": ">= 0.4" + }, "funding": { "url": "https://github.com/sponsors/ljharb" } @@ -3185,9 +3193,9 @@ } }, "node_modules/semver": { - "version": "7.6.2", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.2.tgz", - "integrity": "sha512-FNAIBWCx9qcRhoHcgcJ0gvU7SN1lYU2ZXuSfl04bSC5OpvDHFyJCjdNHomPXxjQlCBU67YW64PzY7/VIEH7F2w==", + "version": "7.6.3", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.3.tgz", + "integrity": "sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==", "bin": { "semver": "bin/semver.js" }, diff --git a/packages/client-sdk-web/package-lock.json b/packages/client-sdk-web/package-lock.json index 65016861b..38870ba6d 100644 --- a/packages/client-sdk-web/package-lock.json +++ b/packages/client-sdk-web/package-lock.json @@ -45,6 +45,7 @@ } }, "../common-integration-tests": { + "name": "@gomomento/common-integration-tests", "version": "0.0.1", "dev": true, "license": "Apache-2.0", @@ -76,6 +77,7 @@ } }, "../core": { + "name": "@gomomento/sdk-core", "version": "0.0.1", "license": "Apache-2.0", "dependencies": { diff --git a/packages/common-integration-tests/src/webhooks.ts b/packages/common-integration-tests/src/webhooks.ts index 0ec20267c..0b388b286 100644 --- a/packages/common-integration-tests/src/webhooks.ts +++ b/packages/common-integration-tests/src/webhooks.ts @@ -1,9 +1,9 @@ import { PostUrlWebhookDestination, - ListWebhooks, - GetWebhookSecret, - TopicPublish, - RotateWebhookSecret, + ListWebhooksResponse, + GetWebhookSecretResponse, + TopicPublishResponse, + RotateWebhookSecretResponse, } from '@gomomento/sdk-core'; import { getWebhookRequestDetails, @@ -73,21 +73,20 @@ export function runWebhookTests( const webhook = testWebhook(integrationTestCacheName); await WithWebhook(topicClient, webhook, async () => { const resp = await topicClient.listWebhooks(integrationTestCacheName); - if (resp instanceof ListWebhooks.Success) { - const webhookWeAreLookingFor = resp - .getWebhooks() - .find( - wh => - wh.id.webhookName === webhook.id.webhookName && - wh.id.cacheName === webhook.id.cacheName - ); - expect(webhookWeAreLookingFor).toBeTruthy(); - } else if (resp instanceof ListWebhooks.Error) { - throw new Error(`list webhooks request failed: ${resp.message()}`); - } else { - throw new Error( - `unknown error occured when making a 'listWebhooks' request: ${resp.toString()}` - ); + switch (resp.type) { + case ListWebhooksResponse.Success: { + const webhookWeAreLookingFor = resp + .getWebhooks() + .find( + wh => + wh.id.webhookName === webhook.id.webhookName && + wh.id.cacheName === webhook.id.cacheName + ); + expect(webhookWeAreLookingFor).toBeTruthy(); + break; + } + case ListWebhooksResponse.Error: + throw new Error(`list webhooks request failed: ${resp.message()}`); } }); }); @@ -98,16 +97,17 @@ export function runWebhookTests( webhook.id.cacheName, webhook.id.webhookName ); - if (resp instanceof GetWebhookSecret.Success) { - expect(resp.secret()).toBeTruthy(); - expect(resp.webhookName()).toEqual(webhook.id.webhookName); - expect(resp.cacheName()).toEqual(webhook.id.cacheName); - } else if (resp instanceof GetWebhookSecret.Error) { - throw new Error(`getWebhookSecret request failed: ${resp.message()}`); - } else { - throw new Error( - `unknown error occured when making a 'getWebhookSecret' request: ${resp.toString()}` - ); + switch (resp.type) { + case GetWebhookSecretResponse.Success: { + expect(resp.secret()).toBeTruthy(); + expect(resp.webhookName()).toEqual(webhook.id.webhookName); + expect(resp.cacheName()).toEqual(webhook.id.cacheName); + break; + } + case GetWebhookSecretResponse.Error: + throw new Error( + `getWebhookSecret request failed: ${resp.message()}` + ); } }); }); @@ -119,7 +119,7 @@ export function runWebhookTests( webhook.topicName, 'a message' ); - if (publishResp instanceof TopicPublish.Error) { + if (publishResp.type === TopicPublishResponse.Error) { throw new Error( `failed to publish to topic: ${webhook.topicName} in cache: ${ webhook.id.cacheName @@ -130,8 +130,10 @@ export function runWebhookTests( } // wait 5 seconds for webhook to get called. Can increase this if needed await delay(5 * 1000); - const detes = await getWebhookRequestDetails(webhook.destination.url()); - expect(detes.invocationCount).toBe(1); + const details = await getWebhookRequestDetails( + webhook.destination.url() + ); + expect(details.invocationCount).toBe(1); }); }); itOnlyInCi('should rotate a webhook secret', async () => { @@ -141,9 +143,9 @@ export function runWebhookTests( webhook.id.cacheName, webhook.id.webhookName ); - if (!(getSecretResp instanceof GetWebhookSecret.Success)) { + if (!(getSecretResp.type === GetWebhookSecretResponse.Success)) { throw new Error( - `unknown error occured when making a 'getWebhookSecret' request: ${getSecretResp.toString()}` + `unknown error occurred when making a 'getWebhookSecret' request: ${getSecretResp.toString()}` ); } expect(getSecretResp.secret()).toBeTruthy(); @@ -151,9 +153,9 @@ export function runWebhookTests( webhook.id.cacheName, webhook.id.webhookName ); - if (!(rotateResp instanceof RotateWebhookSecret.Success)) { + if (!(rotateResp.type === RotateWebhookSecretResponse.Success)) { throw new Error( - `unknown error occured when making a 'rotateWebhookSecret' request: ${rotateResp.toString()}` + `unknown error occurred when making a 'rotateWebhookSecret' request: ${rotateResp.toString()}` ); } expect(rotateResp.secret()).toBeTruthy(); diff --git a/packages/core/src/messages/responses/enums/index.ts b/packages/core/src/messages/responses/enums/index.ts index bdc8dc327..bdcd6688f 100644 --- a/packages/core/src/messages/responses/enums/index.ts +++ b/packages/core/src/messages/responses/enums/index.ts @@ -3,3 +3,4 @@ export * from './cache'; export * from './topics'; export * from './leaderboard'; export * from './store'; +export * from './webhook'; diff --git a/packages/core/src/messages/responses/enums/webhook/index.ts b/packages/core/src/messages/responses/enums/webhook/index.ts new file mode 100644 index 000000000..246812c20 --- /dev/null +++ b/packages/core/src/messages/responses/enums/webhook/index.ts @@ -0,0 +1,24 @@ +export enum PutWebhookResponse { + Success = 'Success', + Error = 'Error', +} + +export enum DeleteWebhookResponse { + Success = 'Success', + Error = 'Error', +} + +export enum ListWebhooksResponse { + Success = 'Success', + Error = 'Error', +} + +export enum RotateWebhookSecretResponse { + Success = 'Success', + Error = 'Error', +} + +export enum GetWebhookSecretResponse { + Success = 'Success', + Error = 'Error', +} diff --git a/packages/core/src/messages/responses/webhook/delete-webhook.ts b/packages/core/src/messages/responses/webhook/delete-webhook.ts index 0d9543ba8..beced5c33 100644 --- a/packages/core/src/messages/responses/webhook/delete-webhook.ts +++ b/packages/core/src/messages/responses/webhook/delete-webhook.ts @@ -1,38 +1,16 @@ import {SdkError} from '../../../errors'; -import {ResponseBase, ResponseError, ResponseSuccess} from '../response-base'; +import {BaseResponseError, BaseResponseSuccess} from '../response-base'; +import {DeleteWebhookResponse} from '../enums'; -/** - * Parent response type for a delete webhook request. The - * response object is resolved to a type-safe object of one of - * the following subtypes: - * - * - {Success} - * - {Error} - * - * `instanceof` type guards can be used to operate on the appropriate subtype. - * @example - * For example: - * ``` - * if (response instanceof DeleteWebhook.Error) { - * // Handle error as appropriate. The compiler will smart-cast `response` to type - * // `DeleteWebhook.Error` in this block, so you will have access to the properties - * // of the Error class; e.g. `response.errorCode()`. - * } - * ``` - */ -export abstract class Response extends ResponseBase {} - -class _Success extends Response {} +interface IResponse { + readonly type: DeleteWebhookResponse; +} /** * Indicates a Successful delete webhook request. */ -export class Success extends ResponseSuccess(_Success) {} - -class _Error extends Response { - constructor(protected _innerException: SdkError) { - super(); - } +export class Success extends BaseResponseSuccess implements IResponse { + readonly type = DeleteWebhookResponse.Success; } /** @@ -45,4 +23,11 @@ class _Error extends Response { * - `message()` - a human-readable description of the error * - `innerException()` - the original error that caused the failure; can be re-thrown. */ -export class Error extends ResponseError(_Error) {} +export class Error extends BaseResponseError implements IResponse { + readonly type: DeleteWebhookResponse.Error = DeleteWebhookResponse.Error; + constructor(_innerException: SdkError) { + super(_innerException); + } +} + +export type Response = Success | Error; diff --git a/packages/core/src/messages/responses/webhook/get-webhook-secret.ts b/packages/core/src/messages/responses/webhook/get-webhook-secret.ts index 648b57ff3..4ad0eec64 100644 --- a/packages/core/src/messages/responses/webhook/get-webhook-secret.ts +++ b/packages/core/src/messages/responses/webhook/get-webhook-secret.ts @@ -1,26 +1,10 @@ import {SdkError} from '../../../errors'; -import {ResponseBase, ResponseError, ResponseSuccess} from '../response-base'; +import {BaseResponseError, BaseResponseSuccess} from '../response-base'; +import {GetWebhookSecretResponse} from '../enums'; -/** - * Parent response type for a GetWebhookSecret request. The - * response object is resolved to a type-safe object of one of - * the following subtypes: - * - * - {Success} - * - {Error} - * - * `instanceof` type guards can be used to operate on the appropriate subtype. - * @example - * For example: - * ``` - * if (response instanceof GetWebhookSecret.Error) { - * // Handle error as appropriate. The compiler will smart-cast `response` to type - * // `GetWebhookSecret.Error` in this block, so you will have access to the properties - * // of the Error class; e.g. `response.errorCode()`. - * } - * ``` - */ -export abstract class Response extends ResponseBase {} +interface IResponse { + readonly type: GetWebhookSecretResponse; +} type Props = { secret: string; @@ -28,7 +12,12 @@ type Props = { webhookName: string; }; -class _Success extends Response { +/** + * Indicates a Successful GetWebhookSecret request. + */ +export class Success extends BaseResponseSuccess implements IResponse { + readonly type: GetWebhookSecretResponse.Success = + GetWebhookSecretResponse.Success; private readonly _secret: string; private readonly _webhookName: string; private readonly _cacheName: string; @@ -50,16 +39,9 @@ class _Success extends Response { cacheName(): string { return this._cacheName; } -} -/** - * Indicates a Successful GetWebhookSecret request. - */ -export class Success extends ResponseSuccess(_Success) {} - -class _Error extends Response { - constructor(protected _innerException: SdkError) { - super(); + public override toString() { + return super.toString() + ': ' + this._webhookName; } } @@ -73,4 +55,12 @@ class _Error extends Response { * - `message()` - a human-readable description of the error * - `innerException()` - the original error that caused the failure; can be re-thrown. */ -export class Error extends ResponseError(_Error) {} +export class Error extends BaseResponseError implements IResponse { + readonly type: GetWebhookSecretResponse.Error = + GetWebhookSecretResponse.Error; + constructor(_innerException: SdkError) { + super(_innerException); + } +} + +export type Response = Success | Error; diff --git a/packages/core/src/messages/responses/webhook/list-webhooks.ts b/packages/core/src/messages/responses/webhook/list-webhooks.ts index f2ee8cf25..ce7cf78f6 100644 --- a/packages/core/src/messages/responses/webhook/list-webhooks.ts +++ b/packages/core/src/messages/responses/webhook/list-webhooks.ts @@ -1,63 +1,40 @@ import {SdkError} from '../../../errors'; -import {ResponseBase, ResponseError, ResponseSuccess} from '../response-base'; +import {BaseResponseError, BaseResponseSuccess} from '../response-base'; import {Webhook} from '../../webhook'; +import {ListWebhooksResponse} from '../enums'; + +interface IResponse { + readonly type: ListWebhooksResponse; +} /** - * Parent response type for a list webhooks request. The - * response object is resolved to a type-safe object of one of - * the following subtypes: - * - * - {Success} - * - {Error} - * - * `instanceof` type guards can be used to operate on the appropriate subtype. - * @example - * For example: - * ``` - * if (response instanceof ListWebhooks.Error) { - * // Handle error as appropriate. The compiler will smart-cast `response` to type - * // `ListWebhooks.Error` in this block, so you will have access to the properties - * // of the Error class; e.g. `response.errorCode()`. - * } - * ``` + * Indicates a Successful list webhooks request. */ -export abstract class Response extends ResponseBase {} - -class _Success extends Response { - private readonly webhooks: Webhook[]; +export class Success extends BaseResponseSuccess implements IResponse { + readonly type: ListWebhooksResponse.Success = ListWebhooksResponse.Success; + private readonly _webhooks: Webhook[]; constructor(webhooks: Webhook[]) { super(); - this.webhooks = webhooks; + this._webhooks = webhooks; } /** * An array of webhooks. * @returns {Webhook[]} */ - public getWebhooks() { - return this.webhooks; + public getWebhooks(): Webhook[] { + return this._webhooks; } public override toString() { return ( super.toString() + ': ' + - this.webhooks.map(webhook => webhook.id.webhookName).join(', ') + this._webhooks.map(webhook => webhook.id.webhookName).join(', ') ); } } -/** - * Indicates a Successful list webhooks request. - */ -export class Success extends ResponseSuccess(_Success) {} - -class _Error extends Response { - constructor(protected _innerException: SdkError) { - super(); - } -} - /** * Indicates that an error occurred during the list webhooks request. * @@ -68,4 +45,11 @@ class _Error extends Response { * - `message()` - a human-readable description of the error * - `innerException()` - the original error that caused the failure; can be re-thrown. */ -export class Error extends ResponseError(_Error) {} +export class Error extends BaseResponseError implements IResponse { + readonly type: ListWebhooksResponse.Error = ListWebhooksResponse.Error; + constructor(_innerException: SdkError) { + super(_innerException); + } +} + +export type Response = Success | Error; diff --git a/packages/core/src/messages/responses/webhook/put-webhook.ts b/packages/core/src/messages/responses/webhook/put-webhook.ts index 9dec358b5..bef35f59a 100644 --- a/packages/core/src/messages/responses/webhook/put-webhook.ts +++ b/packages/core/src/messages/responses/webhook/put-webhook.ts @@ -1,29 +1,18 @@ import {SdkError} from '../../../errors'; -import {ResponseBase, ResponseError, ResponseSuccess} from '../response-base'; +import {BaseResponseError, BaseResponseSuccess} from '../response-base'; +import {PutWebhookResponse} from '../enums'; + +interface IResponse { + readonly type: PutWebhookResponse; +} /** - * Parent response type for a PutWebhook request. The - * response object is resolved to a type-safe object of one of - * the following subtypes: - * - * - {Success} - * - {Error} - * - * `instanceof` type guards can be used to operate on the appropriate subtype. - * @example - * For example: - * ``` - * if (response instanceof PutWebhook.Error) { - * // Handle error as appropriate. The compiler will smart-cast `response` to type - * // `PutWebhook.Error` in this block, so you will have access to the properties - * // of the Error class; e.g. `response.errorCode()`. - * } - * ``` + * Indicates a Successful PutWebhook request. */ -export abstract class Response extends ResponseBase {} - -class _Success extends Response { +export class Success extends BaseResponseSuccess implements IResponse { + readonly type = PutWebhookResponse.Success; private readonly _secretString: string; + constructor(secretString: string) { super(); this._secretString = secretString; @@ -34,17 +23,6 @@ class _Success extends Response { } } -/** - * Indicates a Successful PutWebhook request. - */ -export class Success extends ResponseSuccess(_Success) {} - -class _Error extends Response { - constructor(protected _innerException: SdkError) { - super(); - } -} - /** * Indicates that an error occurred during the PutWebhook request. * @@ -55,4 +33,11 @@ class _Error extends Response { * - `message()` - a human-readable description of the error * - `innerException()` - the original error that caused the failure; can be re-thrown. */ -export class Error extends ResponseError(_Error) {} +export class Error extends BaseResponseError implements IResponse { + readonly type: PutWebhookResponse.Error = PutWebhookResponse.Error; + constructor(_innerException: SdkError) { + super(_innerException); + } +} + +export type Response = Success | Error; diff --git a/packages/core/src/messages/responses/webhook/rotate-webhook-secret.ts b/packages/core/src/messages/responses/webhook/rotate-webhook-secret.ts index 65ceee64b..c5e732828 100644 --- a/packages/core/src/messages/responses/webhook/rotate-webhook-secret.ts +++ b/packages/core/src/messages/responses/webhook/rotate-webhook-secret.ts @@ -1,26 +1,10 @@ import {SdkError} from '../../../errors'; -import {ResponseBase, ResponseError, ResponseSuccess} from '../response-base'; +import {BaseResponseError, BaseResponseSuccess} from '../response-base'; +import {RotateWebhookSecretResponse} from '../enums'; -/** - * Parent response type for a RotateWebhookSecret request. The - * response object is resolved to a type-safe object of one of - * the following subtypes: - * - * - {Success} - * - {Error} - * - * `instanceof` type guards can be used to operate on the appropriate subtype. - * @example - * For example: - * ``` - * if (response instanceof RotateWebhookSecret.Error) { - * // Handle error as appropriate. The compiler will smart-cast `response` to type - * // `RotateWebhookSecret.Error` in this block, so you will have access to the properties - * // of the Error class; e.g. `response.errorCode()`. - * } - * ``` - */ -export abstract class Response extends ResponseBase {} +interface IResponse { + readonly type: RotateWebhookSecretResponse; +} type Props = { secret: string; @@ -28,7 +12,12 @@ type Props = { webhookName: string; }; -class _Success extends Response { +/** + * Indicates a Successful RotateWebhookSecret request. + */ +export class Success extends BaseResponseSuccess implements IResponse { + readonly type: RotateWebhookSecretResponse.Success = + RotateWebhookSecretResponse.Success; private readonly _secret: string; private readonly _webhookName: string; private readonly _cacheName: string; @@ -50,16 +39,9 @@ class _Success extends Response { cacheName(): string { return this._cacheName; } -} -/** - * Indicates a Successful RotateWebhookSecret request. - */ -export class Success extends ResponseSuccess(_Success) {} - -class _Error extends Response { - constructor(protected _innerException: SdkError) { - super(); + public override toString() { + return super.toString() + ': ' + this._webhookName; } } @@ -73,4 +55,12 @@ class _Error extends Response { * - `message()` - a human-readable description of the error * - `innerException()` - the original error that caused the failure; can be re-thrown. */ -export class Error extends ResponseError(_Error) {} +export class Error extends BaseResponseError implements IResponse { + readonly type: RotateWebhookSecretResponse.Error = + RotateWebhookSecretResponse.Error; + constructor(_innerException: SdkError) { + super(_innerException); + } +} + +export type Response = Success | Error;