Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: Move client into protocol and rename builders in adapters #932

Merged
merged 3 commits into from
Jun 12, 2024
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
28 changes: 13 additions & 15 deletions arcjet-bun/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import core, {
Product,
ArcjetRequest,
ExtraProps,
RemoteClient,
RemoteClientOptions,
createRemoteClient,
Arcjet,
} from "arcjet";
import findIP from "@arcjet/ip";
Expand All @@ -24,6 +21,7 @@ import {
platform,
} from "@arcjet/env";
import { Logger } from "@arcjet/logger";
import { createClient } from "@arcjet/protocol/client.js";

// Re-export all named exports from the generic SDK
export * from "arcjet";
Expand Down Expand Up @@ -65,9 +63,12 @@ type PlainObject = {
[key: string]: unknown;
};

export function createBunRemoteClient(
options?: Partial<RemoteClientOptions>,
): RemoteClient {
export type RemoteClientOptions = {
baseUrl?: string;
timeout?: number;
};

export function createRemoteClient(options?: RemoteClientOptions) {
// The base URL for the Arcjet API. Will default to the standard production
// API unless environment variable `ARCJET_BASE_URL` is set.
const url = options?.baseUrl ?? baseUrl(env);
Expand All @@ -77,18 +78,15 @@ export function createBunRemoteClient(
const timeout = options?.timeout ?? (isProduction(env) ? 500 : 1000);

// Transport is the HTTP client that the client uses to make requests.
const transport =
options?.transport ??
createConnectTransport({
baseUrl: url,
httpVersion: "1.1",
});
const transport = createConnectTransport({
baseUrl: url,
httpVersion: "1.1",
});

// TODO(#223): Create separate options type to exclude these
const sdkStack = "BUN";
const sdkVersion = "__ARCJET_SDK_VERSION__";

return createRemoteClient({
return createClient({
transport,
baseUrl: url,
timeout,
Expand Down Expand Up @@ -255,7 +253,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
export default function arcjet<const Rules extends (Primitive | Product)[]>(
options: ArcjetOptions<Rules>,
): ArcjetBun<Simplify<ExtraProps<Rules>>> {
const client = options.client ?? createBunRemoteClient();
const client = options.client ?? createRemoteClient();

const log = options.log
? options.log
Expand Down
1 change: 1 addition & 0 deletions arcjet-bun/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@arcjet/headers": "1.0.0-alpha.14",
"@arcjet/ip": "1.0.0-alpha.14",
"@arcjet/logger": "1.0.0-alpha.14",
"@arcjet/protocol": "1.0.0-alpha.14",
"@connectrpc/connect-node": "1.4.0",
"arcjet": "1.0.0-alpha.14"
},
Expand Down
54 changes: 26 additions & 28 deletions arcjet-next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import arcjet, {
Product,
ArcjetRequest,
ExtraProps,
RemoteClient,
RemoteClientOptions,
createRemoteClient,
Arcjet,
} from "arcjet";
import findIP from "@arcjet/ip";
Expand All @@ -29,6 +26,7 @@ import {
platform,
} from "@arcjet/env";
import { Logger } from "@arcjet/logger";
import { createClient } from "@arcjet/protocol/client.js";

// Re-export all named exports from the generic SDK
export * from "arcjet";
Expand Down Expand Up @@ -70,9 +68,12 @@ type PlainObject = {
[key: string]: unknown;
};

export function createNextRemoteClient(
options?: Partial<RemoteClientOptions>,
): RemoteClient {
export type RemoteClientOptions = {
baseUrl?: string;
timeout?: number;
};

export function createRemoteClient(options?: RemoteClientOptions) {
// The base URL for the Arcjet API. Will default to the standard production
// API unless environment variable `ARCJET_BASE_URL` is set.
const url = options?.baseUrl ?? baseUrl(process.env);
Expand All @@ -84,30 +85,27 @@ export function createNextRemoteClient(
// Transport is the HTTP client that the client uses to make requests.
// The Connect Node client doesn't work on edge runtimes: https://github.com/bufbuild/connect-es/pull/589
// so set the transport using connect-web. The interceptor is required for it work in the edge runtime.
const transport =
options?.transport ??
createConnectTransport({
baseUrl: url,
interceptors: [
/**
* Ensures redirects are followed to properly support the Next.js/Vercel Edge
* Runtime.
* @see
* https://github.com/connectrpc/connect-es/issues/749#issuecomment-1693507516
*/
(next) => (req) => {
req.init.redirect = "follow";
return next(req);
},
],
fetch,
});

// TODO(#223): Create separate options type to exclude these
const transport = createConnectTransport({
baseUrl: url,
interceptors: [
/**
* Ensures redirects are followed to properly support the Next.js/Vercel Edge
* Runtime.
* @see
* https://github.com/connectrpc/connect-es/issues/749#issuecomment-1693507516
*/
(next) => (req) => {
req.init.redirect = "follow";
return next(req);
},
],
fetch,
});

const sdkStack = "NEXTJS";
const sdkVersion = "__ARCJET_SDK_VERSION__";

return createRemoteClient({
return createClient({
transport,
baseUrl: url,
timeout,
Expand Down Expand Up @@ -337,7 +335,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
export default function arcjetNext<const Rules extends (Primitive | Product)[]>(
options: ArcjetOptions<Rules>,
): ArcjetNext<Simplify<ExtraProps<Rules>>> {
const client = options.client ?? createNextRemoteClient();
const client = options.client ?? createRemoteClient();

const log = options.log
? options.log
Expand Down
1 change: 1 addition & 0 deletions arcjet-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@arcjet/headers": "1.0.0-alpha.14",
"@arcjet/ip": "1.0.0-alpha.14",
"@arcjet/logger": "1.0.0-alpha.14",
"@arcjet/protocol": "1.0.0-alpha.14",
"@connectrpc/connect-web": "1.4.0",
"arcjet": "1.0.0-alpha.14"
},
Expand Down
28 changes: 13 additions & 15 deletions arcjet-node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import core, {
Product,
ArcjetRequest,
ExtraProps,
RemoteClient,
RemoteClientOptions,
createRemoteClient,
Arcjet,
} from "arcjet";
import findIP from "@arcjet/ip";
Expand All @@ -21,6 +18,7 @@ import {
platform,
} from "@arcjet/env";
import { Logger } from "@arcjet/logger";
import { createClient } from "@arcjet/protocol/client.js";

// Re-export all named exports from the generic SDK
export * from "arcjet";
Expand Down Expand Up @@ -62,9 +60,12 @@ type PlainObject = {
[key: string]: unknown;
};

export function createNodeRemoteClient(
options?: Partial<RemoteClientOptions>,
): RemoteClient {
export type RemoteClientOptions = {
baseUrl?: string;
timeout?: number;
};

export function createRemoteClient(options?: RemoteClientOptions) {
// The base URL for the Arcjet API. Will default to the standard production
// API unless environment variable `ARCJET_BASE_URL` is set.
const url = options?.baseUrl ?? baseUrl(process.env);
Expand All @@ -74,18 +75,15 @@ export function createNodeRemoteClient(
const timeout = options?.timeout ?? (isProduction(process.env) ? 500 : 1000);

// Transport is the HTTP client that the client uses to make requests.
const transport =
options?.transport ??
createConnectTransport({
baseUrl: url,
httpVersion: "2",
});
const transport = createConnectTransport({
baseUrl: url,
httpVersion: "2",
});

// TODO(#223): Create separate options type to exclude these
const sdkStack = "NODEJS";
const sdkVersion = "__ARCJET_SDK_VERSION__";

return createRemoteClient({
return createClient({
transport,
baseUrl: url,
timeout,
Expand Down Expand Up @@ -249,7 +247,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
export default function arcjet<const Rules extends (Primitive | Product)[]>(
options: ArcjetOptions<Rules>,
): ArcjetNode<Simplify<ExtraProps<Rules>>> {
const client = options.client ?? createNodeRemoteClient();
const client = options.client ?? createRemoteClient();

const log = options.log
? options.log
Expand Down
1 change: 1 addition & 0 deletions arcjet-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@arcjet/headers": "1.0.0-alpha.14",
"@arcjet/ip": "1.0.0-alpha.14",
"@arcjet/logger": "1.0.0-alpha.14",
"@arcjet/protocol": "1.0.0-alpha.14",
"@connectrpc/connect-node": "1.4.0",
"arcjet": "1.0.0-alpha.14"
},
Expand Down
19 changes: 10 additions & 9 deletions arcjet-sveltekit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import core, {
Product,
ArcjetRequest,
ExtraProps,
RemoteClient,
RemoteClientOptions,
createRemoteClient,
Arcjet,
} from "arcjet";
import findIP from "@arcjet/ip";
Expand All @@ -24,6 +21,7 @@ import {
platform,
} from "@arcjet/env";
import { Logger } from "@arcjet/logger";
import { createClient } from "@arcjet/protocol/client.js";

// Re-export all named exports from the generic SDK
export * from "arcjet";
Expand Down Expand Up @@ -93,9 +91,12 @@ function defaultTransport(baseUrl: string) {
}
}

export function createSvelteKitRemoteClient(
options?: Partial<RemoteClientOptions>,
): RemoteClient {
export type RemoteClientOptions = {
baseUrl?: string;
timeout?: number;
};

export function createRemoteClient(options?: RemoteClientOptions) {
// The base URL for the Arcjet API. Will default to the standard production
// API unless environment variable `ARCJET_BASE_URL` is set.
const url = options?.baseUrl ?? baseUrl(env);
Expand All @@ -105,13 +106,13 @@ export function createSvelteKitRemoteClient(
const timeout = options?.timeout ?? (isProduction(env) ? 500 : 1000);

// Transport is the HTTP client that the client uses to make requests.
const transport = options?.transport ?? defaultTransport(url);
const transport = defaultTransport(url);

// TODO(#223): Create separate options type to exclude these
const sdkStack = "SVELTEKIT";
const sdkVersion = "__ARCJET_SDK_VERSION__";

return createRemoteClient({
return createClient({
transport,
baseUrl: url,
timeout,
Expand Down Expand Up @@ -255,7 +256,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
export default function arcjet<const Rules extends (Primitive | Product)[]>(
options: ArcjetOptions<Rules>,
): ArcjetSvelteKit<Simplify<ExtraProps<Rules>>> {
const client = options.client ?? createSvelteKitRemoteClient();
const client = options.client ?? createRemoteClient();

const log = options.log
? options.log
Expand Down
1 change: 1 addition & 0 deletions arcjet-sveltekit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@arcjet/headers": "1.0.0-alpha.14",
"@arcjet/ip": "1.0.0-alpha.14",
"@arcjet/logger": "1.0.0-alpha.14",
"@arcjet/protocol": "1.0.0-alpha.14",
"@arcjet/runtime": "1.0.0-alpha.14",
"@connectrpc/connect-node": "1.4.0",
"@connectrpc/connect-web": "1.4.0",
Expand Down
Loading
Loading