fal-endpoint-types 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fal-endpoint-types",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/rawpixel-vincent/fal-endpoint-types#readme",
6
6
  "bugs": {
package/readme.md CHANGED
@@ -1,4 +1,8 @@
1
- input and output shapes exported from the openapi.json of fal.ai endpoints to typescript types.
1
+ Fal.ai model endpoints Input and Output ts shapes.
2
+ Built from the Open API schemas provided by fal.ai.
3
+
4
+ - https://fal.ai/api/models
5
+ - https://fal.ai/api/openapi/queue/openapi.json?endpoint_id={endpointId}
2
6
 
3
7
  Usage:
4
8
 
@@ -11,46 +15,39 @@ const output: fal.EndpointOutput<'fal-ai/hyper3d/rodin'> = {
11
15
  };
12
16
  ```
13
17
 
14
- ```ts
15
- const getMyMappedEndpointInput = (endpointId: Endpoint, userInputs: any): EndpointInput<typeof endpointId> => {
16
- // ...
17
- // Narrow down the endpoint id
18
- // to infer the type of the returned value
19
- switch (endpointId) {
20
- case 'fal-ai/minimax/hailuo-02/standard/text-to-video':
21
- return {
22
- prompt: userInputs.prompt, // type safe, will be infered by the narrowed endpoint id
23
- };
24
- default:
25
- throw new Error(`Unsupported endpoint: ${endpointId}`);
26
- }
27
- }
28
-
29
- const endpointId = req.endpointId; // 'fal-ai/minimax/hailuo-02/standard/text-to-video'
30
- const input = getMyMappedEndpointInput(endpointId, req.userInputs);
18
+ Usage example:
31
19
 
32
- // --------------------------
33
-
34
- const res = await fetch('https://fal.ai/api/v1/endpoints/{endpointId}', {
35
- method: 'POST',
36
- body: JSON.stringify({
37
- input: {
38
- ...input,
20
+ ```ts
21
+ // ...
22
+ // Narrow down the endpoint id,
23
+ // to infer the type of the returned value,
24
+ // this example uses an index signature.
25
+ // The input type is infered in the function.
26
+ const FalInputs: {
27
+ [x: number | string | symbol]: any;
28
+ } & {
29
+ [K in fal.Endpoint]?: (userInputs: Record<string, any>) => fal.EndpointInput<K>;
30
+ } = {
31
+ 'fal-ai/flux-kontext/dev': (userInputs) => ({
32
+ // typed FluxKontextDevInput
33
+ prompt: typeof userInputs['prompt'] === 'string' ? userInputs['prompt'] : '',
34
+ image_url: typeof userInputs['image_url'] === 'string' ? userInputs['image_url'] : '',
39
35
  }),
40
- }).then(res => res.json());
41
- // check the api urls in the docs, this is just an example.
42
- const result = await fetch('https://fal.ai/api/v1/endpoints/{endpointId}/requests/{res.requestId}', {
43
- method: 'GET',
44
- }).then(res => res.json())
45
- const output = data.data as unknown as EndpointOutput<typeof endpointId>;
46
-
47
- // --------------------------
48
- // OR
49
-
50
- const res = await fal.queue.submit(endpointId, {input});
51
-
52
- // import('@fal/client').Result<EndpointOutput<typeof endpointId>>
53
- const result = await fal.queue.result(res.requestId);
36
+ // '...etc...': (userInputs) => ({}), // more endpoints you want to support
37
+ };
54
38
 
55
- const output = result.data as unknown as EndpointOutput<typeof endpointId>;
39
+ const endpointIdString = String('fal-ai/flux-kontext/dev');
40
+ // any | undefined (best instead of all possible inputs)
41
+ const falInputAny = FalInputs[endpointIdString]?.({ prompt: 'Hello, world!' });
42
+ // FluxKreaTrainerInput | BriaVideoBackgroundRemovalInput
43
+ // | ... 100 more ... | undefined (expensive option, no benefits, not safe)
44
+ const falInputAll =
45
+ isFalEndpoint(endpointIdString) ?
46
+ FalInputs[endpointIdString]?.({ prompt: 'Hello, world!' })
47
+ : undefined;
48
+
49
+ //... Normal inference when the endpoint id is known.
50
+ const endpointId = 'fal-ai/flux-kontext/dev';
51
+ // FluxKontextDevInput | undefined
52
+ const falInput = FalInputs[endpointId]?.({ prompt: 'Hello, world!' });
56
53
  ```
@@ -2,58 +2,9 @@
2
2
 
3
3
  declare global {
4
4
  export namespace fal {
5
- export type Endpoint = keyof fal.endpoints.Endpoints;
6
- /**
7
- * Usage:
8
- * @example
9
- * ```ts
10
- *
11
- * const getMyMappedEndpointInput = (endpointId: Endpoint, userInputs: any): EndpointInput<typeof endpointId> => {
12
- * // ...
13
- * // Narrow down the endpoint id
14
- * // to infer the type of the returned value
15
- * switch (endpointId) {
16
- * case 'fal-ai/minimax/hailuo-02/standard/text-to-video':
17
- * return {
18
- * prompt: userInputs.prompt, // type safe, will be infered by the narrowed endpoint id
19
- * };
20
- * default:
21
- * throw new Error(`Unsupported endpoint: ${endpointId}`);
22
- * }
23
- * }
24
- *
25
- * const endpointId = req.endpointId; // 'fal-ai/minimax/hailuo-02/standard/text-to-video'
26
- * const input = getMyMappedEndpointInput(endpointId, req.userInputs);
27
- *
28
- * // --------------------------
29
- *
30
- * const res = await fetch('https://fal.ai/api/v1/endpoints/{endpointId}', {
31
- * method: 'POST',
32
- * body: JSON.stringify({
33
- * input: {
34
- * ...input,
35
- * }),
36
- * }).then(res => res.json());
37
- * // check the api urls in the docs, this is just an example.
38
- * const result = await fetch('https://fal.ai/api/v1/endpoints/{endpointId}/requests/{res.requestId}', {
39
- * method: 'GET',
40
- * }).then(res => res.json())
41
- * const output = data.data as unknown as EndpointOutput<typeof endpointId>;
42
- *
43
- * // --------------------------
44
- * // OR
45
- *
46
- * const res = await fal.queue.submit(endpointId, {input});
47
- *
48
- * // import('@fal/client').Result<EndpointOutput<typeof endpointId>>
49
- * const result = await fal.queue.result(res.requestId);
50
- *
51
- * const output = result.data as unknown as EndpointOutput<typeof endpointId>;
52
- * ```
53
- *
54
- */
55
- export type EndpointInput<T extends Endpoint> = fal.endpoints.Endpoints[T]['input'];
56
- export type EndpointOutput<T extends Endpoint> = fal.endpoints.Endpoints[T]['output'];
5
+ export type Endpoint = keyof fal.endpoints.Endpoints & string;
6
+ export type EndpointInput<T extends fal.Endpoint> = fal.endpoints.Endpoints[T]['input'];
7
+ export type EndpointOutput<T extends fal.Endpoint> = fal.endpoints.Endpoints[T]['output'];
57
8
  }
58
9
  }
59
10