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

feat(client): allow custom fetch impl #74

Merged
merged 4 commits into from
Jul 22, 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
5 changes: 3 additions & 2 deletions apps/demo-nextjs-app-router/app/queue/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ export default function Home() {
const result: any = await fal.subscribe(endpointId, {
input: JSON.parse(input),
logs: true,
mode: 'streaming',
// pollInterval: 1000,
// mode: "streaming",
mode: 'polling',
pollInterval: 1000,
onQueueUpdate(update) {
console.log('queue update');
console.log(update);
Expand Down
2 changes: 1 addition & 1 deletion libs/client/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@fal-ai/serverless-client",
"description": "The fal serverless JS/TS client",
"version": "0.13.0",
"version": "0.14.0-alpha.1",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
5 changes: 3 additions & 2 deletions libs/client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
credentials?: undefined | string | CredentialsResolver;
proxyUrl?: string;
requestMiddleware?: RequestMiddleware;
responseHandler?: ResponseHandler<any>;

Check warning on line 15 in libs/client/src/config.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
fetch?: typeof fetch;
};

export type RequiredConfig = Required<Config>;
Expand Down Expand Up @@ -64,8 +65,8 @@
configuration = {
...configuration,
requestMiddleware: withMiddleware(
configuration.requestMiddleware,
withProxy({ targetUrl: config.proxyUrl })
withProxy({ targetUrl: config.proxyUrl }),
configuration.requestMiddleware
),
};
}
Expand Down
1 change: 1 addition & 0 deletions libs/client/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export async function dispatchRequest<Input, Output>(
credentials: credentialsValue,
requestMiddleware,
responseHandler,
fetch = global.fetch,
} = getConfig();
const userAgent = isBrowser() ? {} : { 'User-Agent': getUserAgent() };
const credentials =
Expand Down
1 change: 1 addition & 0 deletions libs/client/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* @returns the file extension or `bin` if the content type is not recognized.
*/
function getExtensionFromContentType(contentType: string): string {
const [_, fileType] = contentType.split('/');

Check warning on line 49 in libs/client/src/storage.ts

View workflow job for this annotation

GitHub Actions / build

'_' is assigned a value but never used
return fileType.split(/[-;]/)[0] ?? 'bin';
}

Expand Down Expand Up @@ -76,6 +76,7 @@

export const storageImpl: StorageSupport = {
upload: async (file: Blob) => {
const { fetch = global.fetch } = getConfig();
const { upload_url: uploadUrl, file_url: url } = await initiateUpload(file);
const response = await fetch(uploadUrl, {
method: 'PUT',
Expand All @@ -96,7 +97,7 @@
} else if (input instanceof Blob) {
return await storageImpl.upload(input);
} else if (isPlainObject(input)) {
const inputObject = input as Record<string, any>;

Check warning on line 100 in libs/client/src/storage.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const promises = Object.entries(inputObject).map(
async ([key, value]): Promise<KeyValuePair> => {
return [key, await storageImpl.transformInput(value)];
Expand Down
2 changes: 2 additions & 0 deletions libs/client/src/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createParser } from 'eventsource-parser';
import { getTemporaryAuthToken } from './auth';
import { getConfig } from './config';
import { buildUrl } from './function';
import { ApiError, defaultResponseHandler } from './response';
import { storageImpl } from './storage';
Expand Down Expand Up @@ -35,7 +36,7 @@

type FalStreamEventType = 'message' | 'error' | 'done';

type EventHandler = (event: any) => void;

Check warning on line 39 in libs/client/src/streaming.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

/**
* The class representing a streaming response. With t
Expand Down Expand Up @@ -83,6 +84,7 @@
private start = async () => {
const { url, options } = this;
const { input, method = 'post' } = options;
const { fetch = global.fetch } = getConfig();
try {
const response = await fetch(url, {
method: method.toUpperCase(),
Expand Down Expand Up @@ -169,7 +171,7 @@
return;
};

private handleError = (error: any) => {

Check warning on line 174 in libs/client/src/streaming.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const apiError =
error instanceof ApiError
? error
Expand All @@ -188,7 +190,7 @@
this.listeners.get(type)?.push(listener);
};

private emit = (type: FalStreamEventType, event: any) => {

Check warning on line 193 in libs/client/src/streaming.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const listeners = this.listeners.get(type) || [];
for (const listener of listeners) {
listener(event);
Expand Down Expand Up @@ -234,7 +236,7 @@
* @param options the request options, including the input payload.
* @returns the `FalStream` instance.
*/
export async function stream<Input = Record<string, any>, Output = any>(

Check warning on line 239 in libs/client/src/streaming.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 239 in libs/client/src/streaming.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
appId: string,
options: StreamOptions<Input>
): Promise<FalStream<Input, Output>> {
Expand Down
Loading