Skip to content

Commit

Permalink
Generate stytch-node completely from API definitions + docs
Browse files Browse the repository at this point in the history
  • Loading branch information
logan-stytch committed Jun 15, 2023
1 parent 2f7f879 commit 893a574
Show file tree
Hide file tree
Showing 68 changed files with 7,488 additions and 2,951 deletions.
28 changes: 12 additions & 16 deletions lib/b2b/client.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import { MagicLinks } from "./magic_links";
import { Sessions } from "./sessions";
import { Organizations } from "./organizations";
import { SSO } from "./sso";
import { BaseClient, ClientConfig } from "../shared/client";
import * as jose from "jose";
import { JwtConfig } from "../shared/sessions";
import { BaseClient, ClientConfig } from "../shared/client";
import { Discovery } from "./discovery";
import { JwtConfig } from "../shared/sessions";
import { MagicLinks } from "./magic_links";
import { Organizations } from "./organizations";
import { Passwords } from "./passwords";
import { Sessions } from "./sessions";
import { SSO } from "./sso";

export class B2BClient extends BaseClient {
protected jwtConfig: JwtConfig;
magicLinks: MagicLinks;
sessions: Sessions;
organizations: Organizations;
sso: SSO;
sessions: Sessions;
discovery: Discovery;
magicLinks: MagicLinks;
passwords: Passwords;
sso: SSO;

constructor(config: ClientConfig) {
super(config);

if (!this.fetchConfig.baseURL.endsWith("b2b/")) {
this.fetchConfig.baseURL += "b2b/";
}

this.jwtConfig = {
// Only allow JWTs that were meant for this project.
projectID: config.project_id,
Expand All @@ -33,11 +29,11 @@ export class B2BClient extends BaseClient {
),
};

this.magicLinks = new MagicLinks(this.fetchConfig);
this.sessions = new Sessions(this.fetchConfig, this.jwtConfig);
this.organizations = new Organizations(this.fetchConfig);
this.sso = new SSO(this.fetchConfig);
this.sessions = new Sessions(this.fetchConfig, this.jwtConfig);
this.discovery = new Discovery(this.fetchConfig);
this.magicLinks = new MagicLinks(this.fetchConfig);
this.passwords = new Passwords(this.fetchConfig);
this.sso = new SSO(this.fetchConfig);
}
}
142 changes: 41 additions & 101 deletions lib/b2b/discovery.ts
Original file line number Diff line number Diff line change
@@ -1,112 +1,52 @@
import { MemberSession, ResponseWithMember } from "./shared_b2b";
import { BaseResponse, request, fetchConfig } from "../shared";
import { DiscoveredOrganization } from "./organizations";

export interface B2BDiscoveryOrganizationsRequest {
intermediate_session_token?: string;
session_token?: string;
session_jwt?: string;
}

export interface B2BDiscoveryOrganizationsResponse extends BaseResponse {
email_address: string;
discovered_organizations: DiscoveredOrganization[];
}

export interface B2BDiscoveryOrganizationCreateRequest {
intermediate_session_token: string;
session_duration_minutes?: number;
session_custom_claims?: Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
organization_name?: string;
organization_slug?: string;
organization_logo_url?: string;
trusted_metadata?: Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
sso_jit_provisioning?: "ALL_ALLOWED" | "RESTRICTED" | "NOT_ALLOWED";
email_allowed_domains?: string[];
email_jit_provisioning?: "RESTRICTED" | "NOT_ALLOWED";
email_invites?: "ALL_ALLOWED" | "RESTRICTED" | "NOT_ALLOWED";
auth_methods?: "ALL_ALLOWED" | "RESTRICTED";
allowed_auth_methods?: string[];
}

export interface B2BDiscoveryOrganizationCreateResponse
extends ResponseWithMember {
member_session: MemberSession;
session_token: string;
session_jwt: string;
}

export interface B2BDiscoveryIntermediateSessionExchangeRequest {
intermediate_session_token: string;
organization_id: string;
session_duration_minutes?: number;
session_custom_claims?: Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
}

export interface B2BDiscoveryIntermediateSessionExchangeResponse
extends ResponseWithMember {
member_session: MemberSession;
session_token: string;
session_jwt: string;
}

class Organizations {
private fetchConfig: fetchConfig;

constructor(fetchConfig: fetchConfig) {
this.fetchConfig = fetchConfig;
}

list(
data: B2BDiscoveryOrganizationsRequest
): Promise<B2BDiscoveryOrganizationsResponse> {
return request<B2BDiscoveryOrganizationsResponse>(this.fetchConfig, {
method: "POST",
url: "discovery/organizations",
data,
});
}

create(
data: B2BDiscoveryOrganizationCreateRequest
): Promise<B2BDiscoveryOrganizationCreateResponse> {
return request<B2BDiscoveryOrganizationCreateResponse>(this.fetchConfig, {
method: "POST",
url: "discovery/organizations/create",
data,
});
}
}

class IntermediateSessions {
private fetchConfig: fetchConfig;

constructor(fetchConfig: fetchConfig) {
this.fetchConfig = fetchConfig;
}

exchange(
data: B2BDiscoveryIntermediateSessionExchangeRequest
): Promise<B2BDiscoveryIntermediateSessionExchangeResponse> {
return request<B2BDiscoveryIntermediateSessionExchangeResponse>(
this.fetchConfig,
{
method: "POST",
url: "discovery/intermediate_sessions/exchange",
data,
}
);
}
// !!!
// WARNING: This file is autogenerated
// Only modify code within MANUAL() sections
// or your changes may be overwritten later!
// !!!

import { fetchConfig } from "../shared";
import { IntermediateSessions } from "./discovery_intermediate_sessions";
import { Member } from "./organizations";
import { Organization } from "./organizations";
import { Organizations } from "./discovery_organizations";

/**
* Fields:
* - member_authenticated: Indicates whether or not the discovery magic link initiated session is valid
* for the organization's allowed auth method settings.
* If not, the member needs to perform additional authentication before logging in - such as password or
* SSO auth.
* - organization: The [Organization object](https://stytch.com/docs/b2b/api/organization-object).
* - membership: Information about the membership.
*/
export interface DiscoveredOrganization {
member_authenticated: boolean;
organization?: Organization;
membership?: Membership;
}

/**
* Fields:
* - type: Either `active_member`, `pending_member`, `invited_member`, or
* `eligible_to_join_by_email_domain`
* - details: An object containing additional metadata about the membership, if available.
* - member: The [Member object](https://stytch.com/docs/b2b/api/member-object) if one already exists, or
* null if one does not.
*/
export interface Membership {
type: string;
details?: Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
member?: Member;
}

export class Discovery {
private fetchConfig: fetchConfig;
organizations: Organizations;
intermediateSessions: IntermediateSessions;
organizations: Organizations;

constructor(fetchConfig: fetchConfig) {
this.fetchConfig = fetchConfig;
this.organizations = new Organizations(this.fetchConfig);
this.intermediateSessions = new IntermediateSessions(this.fetchConfig);
this.organizations = new Organizations(this.fetchConfig);
}
}
100 changes: 100 additions & 0 deletions lib/b2b/discovery_intermediate_sessions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// !!!
// WARNING: This file is autogenerated
// Only modify code within MANUAL() sections
// or your changes may be overwritten later!
// !!!

import { fetchConfig } from "../shared";
import { Member, Organization } from "./organizations";
import { MemberSession } from "./sessions";
import { request, BaseResponse } from "../shared";

/**
* Request type for `exchange`.
* Fields:
* - intermediate_session_token: The Intermediate Session Token. This token does not belong to a specific
* instance of a member, but may be exchanged for an existing Member Session or used to create a new
* organization.
* - organization_id: Globally unique UUID that identifies a specific Organization. The `organization_id`
* is critical to perform operations on an Organization, so be sure to preserve this value.
* - session_duration_minutes: Set the session lifetime to be this many minutes from now. This will start
* a new session if one doesn't already exist,
* returning both an opaque `session_token` and `session_jwt` for this session. Remember that the
* `session_jwt` will have a fixed lifetime of
* five minutes regardless of the underlying session duration, and will need to be refreshed over time.
*
* This value must be a minimum of 5 and a maximum of 527040 minutes (366 days).
*
* If a `session_token` or `session_jwt` is provided then a successful authentication will continue to
* extend the session this many minutes.
*
* If the `session_duration_minutes` parameter is not specified, a Stytch session will be created with a
* 60 minute duration. If you don't want
* to use the Stytch session product, you can ignore the session fields in the response.
* - session_custom_claims: Add a custom claims map to the Session being authenticated. Claims are only
* created if a Session is initialized by providing a value in
* `session_duration_minutes`. Claims will be included on the Session object and in the JWT. To update a
* key in an existing Session, supply a new value. To
* delete a key, supply a null value. Custom claims made with reserved claims (`iss`, `sub`, `aud`,
* `exp`, `nbf`, `iat`, `jti`) will be ignored.
* Total custom claims size cannot exceed four kilobytes.
*/
export interface B2BIntermediateSessionsExchangeRequest {
intermediate_session_token: string;
organization_id: string;
session_duration_minutes?: number;
session_custom_claims?: Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
}

/**
* Response type for `exchange`.
* Fields:
* - request_id: Globally unique UUID that is returned with every API call. This value is important to
* log for debugging purposes; we may ask for this value to help identify a specific API call when helping
* you debug an issue.
* - member_id: Globally unique UUID that identifies a specific Member.
* - session_token: A secret token for a given Stytch Session.
* - session_jwt: The JSON Web Token (JWT) for a given Stytch Session.
* - member: The [Member object](https://stytch.com/docs/b2b/api/member-object).
* - organization: The [Organization object](https://stytch.com/docs/b2b/api/organization-object).
* - status_code: The HTTP status code of the response. Stytch follows standard HTTP response status code
* patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX
* are server errors.
* - member_session: The [Session object](https://stytch.com/docs/b2b/api/session-object).
*/
export interface B2BIntermediateSessionsExchangeResponse extends BaseResponse {
request_id: string;
member_id: string;
session_token: string;
session_jwt: string;
member: Member;
organization: Organization;
status_code: number;
member_session?: MemberSession;
}

export class IntermediateSessions {
private fetchConfig: fetchConfig;

constructor(fetchConfig: fetchConfig) {
this.fetchConfig = fetchConfig;
}

/**
* Exchange an Intermediate Session for a fully realized [Member
* Session](https://stytch.com/docs/b2b/api/session-object) in a desired
* [Organization](https://stytch.com/docs/b2b/api/organization-object).
* This operation consumes the Intermediate Session.
*
* This endpoint can be used to accept invites and create new members via domain matching.
*/
exchange(
data: B2BIntermediateSessionsExchangeRequest
): Promise<B2BIntermediateSessionsExchangeResponse> {
return request<B2BIntermediateSessionsExchangeResponse>(this.fetchConfig, {
method: "POST",
url: `/v1/b2b/discovery/intermediate_sessions/exchange`,
data,
});
}
}
Loading

0 comments on commit 893a574

Please sign in to comment.