Skip to content

Commit

Permalink
Improve Task Type
Browse files Browse the repository at this point in the history
  • Loading branch information
sizumita committed Nov 24, 2022
1 parent de43202 commit 4d7924e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
26 changes: 11 additions & 15 deletions src/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class Service<T extends string, E extends Tasks<T>> {
throw new Error("unimplemented")
}

tasks(route: T, data: E[T]["data"]): Parameters {
tasks(route: E): Parameters {
throw new Error("unimplemented")
}

Expand All @@ -32,13 +32,9 @@ export type Parameters = {
}


export type Task<T extends string, D> = {
type: T,
data: D
}

export type Tasks<T extends string> = { [k in T]: Task<k, unknown> }
export type Task<T extends string, D> = [T, D]

export type Tasks<T extends string> = { [k in T]: Task<k, unknown> }[T]

export class Provider<T extends string, E extends Tasks<T>> {
private service: Service<T, E>
Expand All @@ -47,24 +43,24 @@ export class Provider<T extends string, E extends Tasks<T>> {
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))
}
}
13 changes: 6 additions & 7 deletions src/provider/provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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 {
testGet = "/get",
testPost = "/post",
}

interface ApiTasks extends Tasks<ApiRoutes> {
[ApiRoutes.testGet]: Task<ApiRoutes.testGet, null>
[ApiRoutes.testPost]: Task<ApiRoutes.testPost, {key: string}>
}
type ApiTasks =
Task<ApiRoutes.testGet, null> |
Task<ApiRoutes.testPost, {key: string}>


class ApiService extends Service<ApiRoutes, ApiTasks> {
Expand All @@ -29,7 +28,7 @@ class ApiService extends Service<ApiRoutes, ApiTasks> {
}
}

tasks(route: ApiRoutes, data: ApiTasks[ApiRoutes]["data"]): Parameters {
tasks([route, data]: ApiTasks): Parameters {
switch (route) {
case ApiRoutes.testGet:
return {}
Expand All @@ -42,6 +41,6 @@ class ApiService extends Service<ApiRoutes, ApiTasks> {

test("Test provider `testGet`", async () => {
const provider = new Provider<ApiRoutes, ApiTasks>(new ApiService())
const response = await provider.request(ApiRoutes.testGet, null)
const response = await provider.request([ApiRoutes.testGet, null])
expect(response.ok).toBe(true)
})

0 comments on commit 4d7924e

Please sign in to comment.