Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
B2B Client
  • Loading branch information
max-stytch committed Feb 14, 2023
commit ab2f801839228c98a980d350a5a091ae6ce75d93
75 changes: 75 additions & 0 deletions dist/b2b/client.js

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

2 changes: 1 addition & 1 deletion dist/b2c/client.js

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

22 changes: 20 additions & 2 deletions dist/index.js

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

File renamed without changes.
76 changes: 76 additions & 0 deletions lib/b2b/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import * as http from "http";
import { btoa } from "isomorphic-base64";
import * as envs from "../shared/envs";
import { MagicLinks } from "./magic_links";
import { Sessions } from "./sessions";
import { Organizations } from "./organizations";
import { fetchConfig } from "../shared";
import { SSO } from "./sso";
import { version } from "../../package.json";

const DEFAULT_TIMEOUT = 10 * 60 * 1000; // Ten minutes

interface Config {
project_id: string;
secret: string;
env: string;
timeout?: number;
agent?: http.Agent;
}

export class B2BClient {
magicLinks: MagicLinks;
sessions: Sessions;
organizations: Organizations;
sso: SSO;

private fetchConfig: fetchConfig;

constructor(config: Config) {
if (typeof config != "object") {
throw new Error(
"Unexpected config type. Refer to https://github.com/stytchauth/stytch-node for how to use the Node client library."
);
}

if (!config.project_id) {
throw new Error('Missing "project_id" in config');
}

if (!config.secret) {
throw new Error('Missing "secret" in config');
}

if (!config.env) {
throw new Error('Missing "env" in config');
}

if (config.env != envs.test && config.env != envs.live) {
// TODO: warn about non-production configuration
}

const headers = {
"Content-Type": "application/json",
"User-Agent": `Stytch Node v${version}`,
Authorization: "Basic " + btoa(config.project_id + ":" + config.secret),
};

this.fetchConfig = {
baseURL: config.env,
headers,
timeout: config.timeout || DEFAULT_TIMEOUT,
agent: config.agent,
};

// Get a baseURL that ends with a slash to make building route URLs easier.
let baseURL = config.env;
if (!baseURL.endsWith("/")) {
baseURL += "/";
}

this.magicLinks = new MagicLinks(this.fetchConfig);
this.sessions = new Sessions(this.fetchConfig);
this.organizations = new Organizations(this.fetchConfig);
this.sso = new SSO(this.fetchConfig);
}
}
2 changes: 1 addition & 1 deletion lib/b2c/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { btoa } from "isomorphic-base64";
import * as jose from "jose";
import { version } from "../../package.json";
import { CryptoWallets } from "./crypto_wallets";
import * as envs from "./envs";
import * as envs from "../shared/envs";
import { MagicLinks } from "./magic_links";
import { OAuth } from "./oauth";
import { OTPs } from "./otps";
Expand Down
4 changes: 3 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export { Client } from "./b2c/client";
export * as envs from "./b2c/envs";
export { B2BClient } from "./b2b/client";
export * as envs from "./shared/envs";
export { UserSearchOperator } from "./b2c/users";
export { SearchOperator } from "./b2b/shared_b2b";
export * from "./shared/errors";
File renamed without changes.
39 changes: 39 additions & 0 deletions test/b2b/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as stytch from "../../lib";

describe("config errors", () => {
test("config is not an object", () => {
expect(() => {
new stytch.B2BClient(0 as any); // eslint-disable-line @typescript-eslint/no-explicit-any
}).toThrow(/Unexpected config type/);
});

test("missing project ID", () => {
expect(() => {
new stytch.B2BClient({
project_id: "",
secret: "secret-test-11111111-1111-4111-8111-111111111111",
env: stytch.envs.test,
});
}).toThrow(/Missing "project_id" in config/);
});

test("missing secret", () => {
expect(() => {
new stytch.B2BClient({
project_id: "project-test-00000000-0000-4000-8000-000000000000",
secret: "",
env: stytch.envs.test,
});
}).toThrow(/Missing "secret" in config/);
});

test("missing environment", () => {
expect(() => {
new stytch.B2BClient({
project_id: "project-test-00000000-0000-4000-8000-000000000000",
secret: "secret-test-11111111-1111-4111-8111-111111111111",
env: "",
});
}).toThrow(/Missing "env" in config/);
});
});
22 changes: 22 additions & 0 deletions types/lib/b2b/client.d.ts

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

4 changes: 3 additions & 1 deletion types/lib/index.d.ts

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

File renamed without changes.