diff --git a/src/provider/index.ts b/src/provider/index.ts index 5c5c2dc..ba2c3b4 100644 --- a/src/provider/index.ts +++ b/src/provider/index.ts @@ -15,7 +15,7 @@ export class Service> { throw new Error("unimplemented") } - tasks(route: T, data: E[T]["data"]): Parameters { + tasks(route: E): Parameters { throw new Error("unimplemented") } @@ -32,13 +32,9 @@ export type Parameters = { } -export type Task = { - type: T, - data: D -} - -export type Tasks = { [k in T]: Task } +export type Task = [T, D] +export type Tasks = { [k in T]: Task }[T] export class Provider> { private service: Service @@ -47,24 +43,24 @@ export class Provider> { this.service = service } - async request(route: T, value: E[T]["data"]) { + async request(route: E) { const f = typeof this.service.fetcher === "undefined" ? kuiper : kuiper(this.service.fetcher) - const task = this.service.tasks(route, value) - let parsedRoute = route as string + const task = this.service.tasks(route) + let parsedRoutePath = route[0] as string // replace url params for (const [key, value] of Object.entries(task?.params ?? {})) { - parsedRoute = route.replaceAll(`{${key}}`, value) + parsedRoutePath = parsedRoutePath.replaceAll(`{${key}}`, value) } - const url = new URL(this.service.baseUrl + parsedRoute) + const url = new URL(this.service.baseUrl + parsedRoutePath) // set queries Object.entries(task?.queries ?? {}).map(([key, value]) => url.searchParams.set(key, value)) - return await f(this.service.baseUrl + route, - makeOptionWithBody(this.service.methods(route), { - headers: this.service.headers(route), + return await f(this.service.baseUrl + parsedRoutePath, + makeOptionWithBody(this.service.methods(route[0]), { + headers: this.service.headers(route[0]), }, task?.body)) } } diff --git a/src/provider/provider.test.ts b/src/provider/provider.test.ts index 4f3ed32..8732b22 100644 --- a/src/provider/provider.test.ts +++ b/src/provider/provider.test.ts @@ -1,5 +1,5 @@ import type {Method} from "../kuiper" -import type { Task, Tasks,Parameters} from "./index" +import type {Task, Parameters} from "./index" import { Provider, Service} from "./index" enum ApiRoutes { @@ -7,10 +7,9 @@ enum ApiRoutes { testPost = "/post", } -interface ApiTasks extends Tasks { - [ApiRoutes.testGet]: Task - [ApiRoutes.testPost]: Task -} +type ApiTasks = + Task | + Task class ApiService extends Service { @@ -29,7 +28,7 @@ class ApiService extends Service { } } - tasks(route: ApiRoutes, data: ApiTasks[ApiRoutes]["data"]): Parameters { + tasks([route, data]: ApiTasks): Parameters { switch (route) { case ApiRoutes.testGet: return {} @@ -42,6 +41,6 @@ class ApiService extends Service { test("Test provider `testGet`", async () => { const provider = new Provider(new ApiService()) - const response = await provider.request(ApiRoutes.testGet, null) + const response = await provider.request([ApiRoutes.testGet, null]) expect(response.ok).toBe(true) })