From 408943b0cfb4f8cb96720a048d8d81f48847b380 Mon Sep 17 00:00:00 2001 From: Petr Pavlik Date: Wed, 23 Oct 2024 21:26:45 +0200 Subject: [PATCH] Update package.json version to 1.1.0 --- .vscode/launch.json | 16 ++++++++++++++++ package.json | 2 +- src/errors.ts | 9 +++++++++ src/index.ts | 5 +++-- src/indiepitcher.test.ts | 10 ++++++++++ 5 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 src/errors.ts diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ebbe656 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Run NPM Test", + "runtimeExecutable": "npm", + "runtimeArgs": ["run", "test"], + "skipFiles": ["/**"] + } + ] +} diff --git a/package.json b/package.json index e6a1f24..5aa4212 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "indiepitcher", - "version": "1.0.1", + "version": "1.1.0", "main": "./dist/index.js", "module": "./dist/index.mjs", "types": "./dist/index.d.ts", diff --git a/src/errors.ts b/src/errors.ts new file mode 100644 index 0000000..8322d42 --- /dev/null +++ b/src/errors.ts @@ -0,0 +1,9 @@ +export class IndiePitcherResponseError extends Error { + statusCode: number; + + constructor(message: string, statusCode: number) { + super(message); + this.name = 'IndiePitcherError'; + this.statusCode = statusCode; + } +} diff --git a/src/index.ts b/src/index.ts index e8cb3ec..4169fb4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ import type { SendEmailToContact, SendEmailToMailingList, } from './dtos'; +import { IndiePitcherResponseError } from './errors'; import type { DataResponse, EmptyResponse, @@ -32,8 +33,8 @@ export class IndiePitcher { const response = await fetch(`${this.baseUrl}${path}`, options); if (!response.ok) { - const error = await response.json(); - throw new Error(error); + const json = await response.json(); + throw new IndiePitcherResponseError(json.reason ?? 'Unknown reason', response.status); } const data = await response.json(); diff --git a/src/indiepitcher.test.ts b/src/indiepitcher.test.ts index 96009f5..e06594d 100644 --- a/src/indiepitcher.test.ts +++ b/src/indiepitcher.test.ts @@ -1,4 +1,5 @@ import { IndiePitcher } from '.'; +import type { IndiePitcherResponseError } from './errors'; require('dotenv').config(); const apiKey = process.env.API_KEY; @@ -10,6 +11,15 @@ afterEach(() => { return new Promise((resolve) => setTimeout(resolve, 1000)); }); +test('invalid API key', async () => { + const indiePitcher = new IndiePitcher('xxx'); + try { + await indiePitcher.listMailingLists(); + } catch (error) { + expect((error as IndiePitcherResponseError).message).toBe('Unauthorized'); + } +}); + test('list mailing lists', async () => { const data = await indiePitcher.listMailingLists(); expect(data.data.length).toBe(2);