diff --git a/CHANGELOG.md b/CHANGELOG.md index faf0955..1ad19f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## [1.1.0-alpha.1](https://github.com/prismicio/prismic-custom-types-client/compare/v1.0.2...v1.1.0-alpha.1) (2023-03-27) + + +### Features + +* support `401 Unauthorized` errors ([937aabd](https://github.com/prismicio/prismic-custom-types-client/commit/937aabd51843e0751c6a40e66f8e2a5d459459bd)) + ### [1.0.2](https://github.com/prismicio/prismic-custom-types-client/compare/v1.0.1...v1.0.2) (2022-12-19) diff --git a/package-lock.json b/package-lock.json index dbdfd7e..c1dd1e0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@prismicio/custom-types-client", - "version": "1.0.2", + "version": "1.1.0-alpha.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@prismicio/custom-types-client", - "version": "1.0.2", + "version": "1.1.0-alpha.1", "license": "Apache-2.0", "dependencies": { "@prismicio/types": "^0.2.4" diff --git a/package.json b/package.json index 1d48afe..9b61f17 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@prismicio/custom-types-client", - "version": "1.0.2", + "version": "1.1.0-alpha.1", "description": "JavaScript client to interact with the Prismic Custom Types API", "keywords": [ "typescript", diff --git a/src/client.ts b/src/client.ts index 2328990..8a08fa8 100644 --- a/src/client.ts +++ b/src/client.ts @@ -7,6 +7,7 @@ import { ConflictError, NotFoundError, ForbiddenError, + UnauthorizedError, InvalidPayloadError, } from "./errors"; @@ -459,6 +460,14 @@ export class CustomTypesClient { throw new InvalidPayloadError(text, { url, response: text }); } + // Unauthorized + // - User does not have access to requested repository + case 401: { + const text = await res.text(); + + throw new UnauthorizedError(text, { url, response: text }); + } + // Forbidden // - Missing token // - Incorrect token diff --git a/src/errors.ts b/src/errors.ts index 7f09481..ded5279 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -58,6 +58,11 @@ export interface ForbiddenErrorAPIResponse { * Represents an error when making an unauthorized Prismic Custom Types API * request. */ +export class UnauthorizedError extends PrismicError {} + +/** + * Represents an error when making a forbidden Prismic Custom Types API request. + */ export class ForbiddenError extends PrismicError {} /** diff --git a/src/index.ts b/src/index.ts index 56ae846..0569400 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ export { PrismicError, ConflictError, ForbiddenError, + UnauthorizedError, InvalidPayloadError, MissingFetchError, NotFoundError, diff --git a/test/client.test.ts b/test/client.test.ts index fd32b9e..25ac610 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -152,7 +152,26 @@ test("doesn't duplicate endpoint trailing slash", async (ctx) => { ); }); -test("throws ForbiddenError if unauthorized", async (ctx) => { +test("throws UnauthorizedError if unauthorized", async (ctx) => { + const client = createClient(ctx); + + ctx.server.use( + msw.rest.get( + new URL("./customtypes", client.endpoint).toString(), + (_req, res, ctx) => { + // We force the API to return a 401 status code to simulate an + // unauthorized request. + return res(ctx.status(401), ctx.text("[MOCK UNAUTHORIZED ERROR]")); + }, + ), + ); + + await expect(async () => { + await client.getAllCustomTypes(); + }).rejects.toThrow(prismicCustomTypes.UnauthorizedError); +}); + +test("throws ForbiddenError if forbidden", async (ctx) => { const client = createClient(ctx); ctx.server.use(