Skip to content

Commit a53983c

Browse files
authored
Add user not found error (#27)
1 parent a46b3ec commit a53983c

File tree

4 files changed

+112
-13
lines changed

4 files changed

+112
-13
lines changed

src/connectionsv1.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface Challenge {
88
/**
99
* The challenge string.
1010
*/
11-
challenge?: string;
11+
challenge: string;
1212
}
1313

1414
/**
@@ -62,7 +62,7 @@ export interface DeleteCustomUserRequest {
6262
/**
6363
* The external identifier for the user.
6464
*/
65-
id?: string;
65+
id: string;
6666
}
6767

6868
/**
@@ -76,7 +76,7 @@ export interface ErrorResponse {
7676
/**
7777
* A user-facing error message.
7878
*/
79-
message?: string;
79+
message: string;
8080
}
8181

8282
/**
@@ -86,7 +86,7 @@ export interface GetCustomUserRequest {
8686
/**
8787
* The external identifier for the user.
8888
*/
89-
id?: string;
89+
id: string;
9090
}
9191

9292
/**
@@ -97,7 +97,7 @@ export interface ListCustomUsersRequest {
9797
* The maximum number of users to return. The webhook is allowed to
9898
* return fewer than this value, but it should never return more.
9999
*/
100-
pageSize?: number;
100+
pageSize: number;
101101
/**
102102
* A page token, this is from the response of the previous list
103103
* request.

src/mod.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type * as userv1 from "./userv1.ts";
1313
export { AdminApi } from "./clients.ts";
1414
export { UserApi } from "./clients.ts";
1515
export { Webhook } from "./webhook/actions.ts";
16+
export { WebhookUserNotFound } from "./webhook/errors.ts";
1617
export { WebhookResponse, WebhookRequest } from "./webhook/http.ts";
1718

1819
// misc

src/webhook/errors.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Code } from "../code.ts";
2+
import { UserHubError } from "../error.ts";
3+
4+
/**
5+
* WebhookUserNotFound is an error which can be used to indicate a user was
6+
* not found in the onGetUser, onUpdateUser, and onDeleteUser methods.
7+
*/
8+
export class WebhookUserNotFound extends UserHubError {
9+
constructor() {
10+
super({
11+
message: "User not found",
12+
apiCode: Code.NotFound,
13+
});
14+
}
15+
}

test/node/webhook.test.ts

+91-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
// Code generated. DO NOT EDIT.
22
import * as constants from "../../src/internal/constants.ts";
3-
import { Code, UserHubError, type eventsv1 } from "../../src/mod.ts";
4-
import { Webhook } from "../../src/webhook/actions.ts";
5-
import { concatArrays, loadCrypto } from "../../src/webhook/base.ts";
63
import {
4+
Code,
5+
UserHubError,
76
WebhookRequest,
87
WebhookResponse,
9-
getHeader,
10-
} from "../../src/webhook/http.ts";
8+
WebhookUserNotFound,
9+
type connectionsv1,
10+
type eventsv1,
11+
} from "../../src/mod.ts";
12+
import { Webhook } from "../../src/webhook/actions.ts";
13+
import { concatArrays, loadCrypto } from "../../src/webhook/base.ts";
14+
import { getHeader } from "../../src/webhook/http.ts";
1115
import { expect, test } from "vitest";
1216

1317
test.each<WebhookTest>([
@@ -268,18 +272,97 @@ test.each<WebhookTest>([
268272
setTimestamp: true,
269273
addSignature: true,
270274
},
275+
{
276+
name: "List users",
277+
secret: "test",
278+
request: new WebhookRequest({
279+
headers: {
280+
"UserHub-Action": "users.list",
281+
},
282+
body: '{"pageSize":100}',
283+
}),
284+
response: new WebhookResponse({
285+
statusCode: 200,
286+
body: '{"nextPageToken":"","users":[]}',
287+
}),
288+
setTimestamp: true,
289+
addSignature: true,
290+
},
291+
{
292+
name: "Get user",
293+
secret: "test",
294+
request: new WebhookRequest({
295+
headers: {
296+
"UserHub-Action": "users.get",
297+
},
298+
body: '{"id": "1"}',
299+
}),
300+
response: new WebhookResponse({
301+
statusCode: 200,
302+
body: '{"id":"1","displayName":"","email":"","emailVerified":false,"phoneNumber":"","phoneNumberVerified":false,"imageUrl":"","disabled":false}',
303+
}),
304+
setTimestamp: true,
305+
addSignature: true,
306+
},
307+
{
308+
name: "Get user not found",
309+
secret: "test",
310+
request: new WebhookRequest({
311+
headers: {
312+
"UserHub-Action": "users.get",
313+
},
314+
body: '{"id": "not-found"}',
315+
}),
316+
response: new WebhookResponse({
317+
statusCode: 404,
318+
body: '{"message":"User not found","code":"NOT_FOUND"}',
319+
}),
320+
setTimestamp: true,
321+
addSignature: true,
322+
},
271323
])("handler: $name", async (test) => {
272324
const webhook = new Webhook(test.secret);
273325

274-
webhook.onEvent((event: eventsv1.Event) => {
275-
if (event.type !== "ok") {
326+
webhook.onEvent((input: eventsv1.Event) => {
327+
if (input.type !== "ok") {
276328
throw new UserHubError({
277-
message: `Event failed: ${event.type}`,
329+
message: `Event failed: ${input.type}`,
278330
apiCode: Code.InvalidArgument,
279331
});
280332
}
281333
});
282334

335+
webhook.onListUsers(
336+
(
337+
input: connectionsv1.ListCustomUsersRequest,
338+
): connectionsv1.ListCustomUsersResponse => {
339+
if (input.pageSize !== 100) {
340+
throw new Error(`unexpected page size: ${input.pageSize}`);
341+
}
342+
343+
return { nextPageToken: "", users: [] };
344+
},
345+
);
346+
347+
webhook.onGetUser(
348+
(input: connectionsv1.GetCustomUserRequest): connectionsv1.CustomUser => {
349+
if (input.id === "not-found") {
350+
throw new WebhookUserNotFound();
351+
}
352+
353+
return {
354+
id: input.id,
355+
displayName: "",
356+
email: "",
357+
emailVerified: false,
358+
phoneNumber: "",
359+
phoneNumberVerified: false,
360+
imageUrl: "",
361+
disabled: false,
362+
};
363+
},
364+
);
365+
283366
const encoder = new TextEncoder();
284367

285368
if (test.setTimestamp) {

0 commit comments

Comments
 (0)