Skip to content

Commit

Permalink
Merge pull request #7 from prismicio/feat/support-401-unauthorized-er…
Browse files Browse the repository at this point in the history
…rors
  • Loading branch information
lihbr authored Mar 28, 2023
2 parents d1021a7 + e413fae commit f8ea04c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 9 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ConflictError,
NotFoundError,
ForbiddenError,
UnauthorizedError,
InvalidPayloadError,
} from "./errors";

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export interface ForbiddenErrorAPIResponse {
* Represents an error when making an unauthorized Prismic Custom Types API
* request.
*/
export class UnauthorizedError extends PrismicError<string> {}

/**
* Represents an error when making a forbidden Prismic Custom Types API request.
*/
export class ForbiddenError extends PrismicError<ForbiddenErrorAPIResponse> {}

/**
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
PrismicError,
ConflictError,
ForbiddenError,
UnauthorizedError,
InvalidPayloadError,
MissingFetchError,
NotFoundError,
Expand Down
21 changes: 20 additions & 1 deletion test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit f8ea04c

Please sign in to comment.