Skip to content

Commit 75bba50

Browse files
committed
feat: support additional input from plugins
1 parent cdc2bcf commit 75bba50

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

packages/better-fetch/src/create-fetch/types.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { StandardSchemaV1 } from "../standard-schema";
2-
import { BetterFetchPlugin } from "../plugins";
2+
import type { BetterFetchPlugin } from "../plugins";
33
import type { Prettify, StringLiteralUnion } from "../type-utils";
44
import type { BetterFetchOption, BetterFetchResponse } from "../types";
55
import type { FetchSchema, Schema } from "./schema";
@@ -124,7 +124,11 @@ export type GetKey<S, K> = S extends Schema
124124
? AP
125125
: string
126126
: string
127-
: K
127+
: S["config"]["prefix"] extends string
128+
? K extends `${S["config"]["prefix"]}${infer AP}`
129+
? AP
130+
: K
131+
: K
128132
: K;
129133

130134
export type UnionToIntersection<U> = (
@@ -148,11 +152,9 @@ export type MergeSchema<Options extends CreateFetchOption> =
148152

149153
export type InferPluginOptions<Options extends CreateFetchOption> =
150154
Options["plugins"] extends Array<infer P>
151-
? P extends BetterFetchPlugin
152-
? P["getOptions"] extends () => infer O
153-
? O extends StandardSchemaV1
154-
? UnionToIntersection<StandardSchemaV1.InferOutput<O>>
155-
: {}
155+
? P extends BetterFetchPlugin<infer O>
156+
? O extends Record<string, any>
157+
? UnionToIntersection<O>
156158
: {}
157159
: {}
158160
: {};

packages/better-fetch/src/plugins.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { StandardSchemaV1 } from "./standard-schema";
2-
import { Schema } from "./create-fetch";
3-
import { BetterFetchError } from "./error";
2+
import type { Schema } from "./create-fetch";
3+
import type { BetterFetchError } from "./error";
44
import type { BetterFetchOption } from "./types";
55

66
export type RequestContext<T extends Record<string, any> = any> = {
@@ -80,7 +80,9 @@ export interface FetchHooks<Res = any> {
8080
/**
8181
* A plugin that returns an id and hooks
8282
*/
83-
export type BetterFetchPlugin = {
83+
export type BetterFetchPlugin<
84+
ExtraOptions extends Record<string, any> = Record<string, any>,
85+
> = {
8486
/**
8587
* A unique id for the plugin
8688
*/
@@ -111,7 +113,7 @@ export type BetterFetchPlugin = {
111113
*/
112114
init?: (
113115
url: string,
114-
options?: BetterFetchOption,
116+
options?: BetterFetchOption & ExtraOptions,
115117
) =>
116118
| Promise<{
117119
url: string;
@@ -127,6 +129,15 @@ export type BetterFetchPlugin = {
127129
schema?: Schema;
128130
/**
129131
* Additional options that can be passed to the plugin
132+
*
133+
* @deprecated Use type inference through direct typescript instead
134+
* ```ts
135+
* const plugin = {
136+
*
137+
* } satisfies BetterFetchPlugin<{
138+
* myCustomOptions: string;
139+
* }>
140+
* ```
130141
*/
131142
getOptions?: () => StandardSchemaV1;
132143
};

packages/better-fetch/src/test/create.test.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from "../create-fetch";
1919
import type { BetterFetchResponse } from "../types";
2020

21-
import { BetterFetchPlugin } from "../plugins";
21+
import type { BetterFetchPlugin } from "../plugins";
2222
import { ValidationError } from "../utils";
2323
import { router } from "./test-router";
2424

@@ -455,8 +455,8 @@ describe("plugin", () => {
455455
const plugin = {
456456
id: "test",
457457
name: "Test",
458-
schema: createSchema(
459-
{
458+
schema: {
459+
schema: {
460460
"/path": {
461461
output: z.object({
462462
message: z.string(),
@@ -471,16 +471,11 @@ describe("plugin", () => {
471471
}),
472472
},
473473
},
474-
{
475-
baseURL: "http://localhost:4001",
474+
config: {
476475
prefix: "prefix",
477476
strict: true,
477+
baseURL: "http://localhost:4001",
478478
},
479-
),
480-
getOptions() {
481-
return z.object({
482-
onUpload: z.function(),
483-
});
484479
},
485480
} satisfies BetterFetchPlugin;
486481
const plugin2 = {
@@ -511,16 +506,17 @@ describe("plugin", () => {
511506
plugins: [plugin],
512507
baseURL: "http://localhost:4001",
513508
});
514-
expectTypeOf($fetch)
515-
.parameter(0)
509+
510+
expectTypeOf($fetch.call)
511+
.parameter(1)
516512
.toMatchTypeOf<"prefix/path" | "prefix/path/:param">();
517513
});
518-
519514
it("should infer baseURL", async () => {
520515
const $fetch = createFetch({
521516
plugins: [plugin2],
522517
baseURL: "http://localhost:4001",
523518
});
519+
524520
expectTypeOf($fetch)
525521
.parameter(0)
526522
.toMatchTypeOf<"http://localhost:4001/path">();
@@ -631,7 +627,12 @@ describe("plugin", () => {
631627
{
632628
id: "test",
633629
name: "Test",
634-
},
630+
getOptions() {
631+
return z.object({
632+
onUpload: z.function(),
633+
});
634+
},
635+
} satisfies BetterFetchPlugin,
635636
],
636637
schema: createSchema(
637638
{

0 commit comments

Comments
 (0)