Skip to content
This repository was archived by the owner on Jul 2, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/connectionsv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface Challenge {
/**
* The challenge string.
*/
challenge?: string;
challenge: string;
}

/**
Expand Down Expand Up @@ -62,7 +62,7 @@ export interface DeleteCustomUserRequest {
/**
* The external identifier for the user.
*/
id?: string;
id: string;
}

/**
Expand All @@ -76,7 +76,7 @@ export interface ErrorResponse {
/**
* A user-facing error message.
*/
message?: string;
message: string;
}

/**
Expand All @@ -86,7 +86,7 @@ export interface GetCustomUserRequest {
/**
* The external identifier for the user.
*/
id?: string;
id: string;
}

/**
Expand All @@ -97,7 +97,7 @@ export interface ListCustomUsersRequest {
* The maximum number of users to return. The webhook is allowed to
* return fewer than this value, but it should never return more.
*/
pageSize?: number;
pageSize: number;
/**
* A page token, this is from the response of the previous list
* request.
Expand Down
1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type * as userv1 from "./userv1.ts";
export { AdminApi } from "./clients.ts";
export { UserApi } from "./clients.ts";
export { Webhook } from "./webhook/actions.ts";
export { WebhookUserNotFound } from "./webhook/errors.ts";
export { WebhookResponse, WebhookRequest } from "./webhook/http.ts";

// misc
Expand Down
15 changes: 15 additions & 0 deletions src/webhook/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Code } from "../code.ts";
import { UserHubError } from "../error.ts";

/**
* WebhookUserNotFound is an error which can be used to indicate a user was
* not found in the onGetUser, onUpdateUser, and onDeleteUser methods.
*/
export class WebhookUserNotFound extends UserHubError {
constructor() {
super({
message: "User not found",
apiCode: Code.NotFound,
});
}
}
99 changes: 91 additions & 8 deletions test/node/webhook.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Code generated. DO NOT EDIT.
import * as constants from "../../src/internal/constants.ts";
import { Code, UserHubError, type eventsv1 } from "../../src/mod.ts";
import { Webhook } from "../../src/webhook/actions.ts";
import { concatArrays, loadCrypto } from "../../src/webhook/base.ts";
import {
Code,
UserHubError,
WebhookRequest,
WebhookResponse,
getHeader,
} from "../../src/webhook/http.ts";
WebhookUserNotFound,
type connectionsv1,
type eventsv1,
} from "../../src/mod.ts";
import { Webhook } from "../../src/webhook/actions.ts";
import { concatArrays, loadCrypto } from "../../src/webhook/base.ts";
import { getHeader } from "../../src/webhook/http.ts";
import { expect, test } from "vitest";

test.each<WebhookTest>([
Expand Down Expand Up @@ -268,18 +272,97 @@ test.each<WebhookTest>([
setTimestamp: true,
addSignature: true,
},
{
name: "List users",
secret: "test",
request: new WebhookRequest({
headers: {
"UserHub-Action": "users.list",
},
body: '{"pageSize":100}',
}),
response: new WebhookResponse({
statusCode: 200,
body: '{"nextPageToken":"","users":[]}',
}),
setTimestamp: true,
addSignature: true,
},
{
name: "Get user",
secret: "test",
request: new WebhookRequest({
headers: {
"UserHub-Action": "users.get",
},
body: '{"id": "1"}',
}),
response: new WebhookResponse({
statusCode: 200,
body: '{"id":"1","displayName":"","email":"","emailVerified":false,"phoneNumber":"","phoneNumberVerified":false,"imageUrl":"","disabled":false}',
}),
setTimestamp: true,
addSignature: true,
},
{
name: "Get user not found",
secret: "test",
request: new WebhookRequest({
headers: {
"UserHub-Action": "users.get",
},
body: '{"id": "not-found"}',
}),
response: new WebhookResponse({
statusCode: 404,
body: '{"message":"User not found","code":"NOT_FOUND"}',
}),
setTimestamp: true,
addSignature: true,
},
])("handler: $name", async (test) => {
const webhook = new Webhook(test.secret);

webhook.onEvent((event: eventsv1.Event) => {
if (event.type !== "ok") {
webhook.onEvent((input: eventsv1.Event) => {
if (input.type !== "ok") {
throw new UserHubError({
message: `Event failed: ${event.type}`,
message: `Event failed: ${input.type}`,
apiCode: Code.InvalidArgument,
});
}
});

webhook.onListUsers(
(
input: connectionsv1.ListCustomUsersRequest,
): connectionsv1.ListCustomUsersResponse => {
if (input.pageSize !== 100) {
throw new Error(`unexpected page size: ${input.pageSize}`);
}

return { nextPageToken: "", users: [] };
},
);

webhook.onGetUser(
(input: connectionsv1.GetCustomUserRequest): connectionsv1.CustomUser => {
if (input.id === "not-found") {
throw new WebhookUserNotFound();
}

return {
id: input.id,
displayName: "",
email: "",
emailVerified: false,
phoneNumber: "",
phoneNumberVerified: false,
imageUrl: "",
disabled: false,
};
},
);

const encoder = new TextEncoder();

if (test.setTimestamp) {
Expand Down