Skip to content

Commit

Permalink
fix: optimize type definition of RequestParams
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Oct 11, 2020
1 parent fb9c025 commit 4a810aa
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 22 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@

## Why frourio ?

Even if you write both the front and server in TypeScript, you can't statically type-check the API's sparsity.
Even if you write both the frontend and backend in TypeScript, you can't statically type-check the API's sparsity.

We are always forced to write "Two TypeScript".
We waste a lot of time on dynamic testing using the browser and Docker.
We waste a lot of time on dynamic testing using the browser and server.

<div align="center">
<img src="https://frouriojs.github.io/frourio/assets/images/problem.png" width="1200" alt="Why frourio ?" />
</div>
<br />
<br />

Frourio is a framework for developing web apps quickly and safely in "One TypeScript".
Frourio is a framework for developing web apps quickly and safely in **"One TypeScript"**.

<div align="center">
<img src="https://frouriojs.github.io/frourio/assets/images/architecture.png" width="1200" alt="Architecture of create-frourio-app" />
Expand Down Expand Up @@ -829,7 +829,7 @@ fastify.listen(3000)
## Dependency Injection
Frourio use [frouriojs/Velona](https://github.com/frouriojs/velona) for dependency injection.
Frourio use [frouriojs/velona](https://github.com/frouriojs/velona) for dependency injection.
`server/api/tasks/index.ts`
Expand Down Expand Up @@ -916,9 +916,7 @@ test('dependency injection into controller', async () => {
const limit = 3
const message = 'test message'
const res = await injectedController.get({
query: { limit, message },
body: undefined,
headers: undefined
query: { limit, message }
})
expect(res.body).toHaveLength(limit)
Expand Down
4 changes: 1 addition & 3 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ test('controller dependency injection', async () => {

await expect(
injectedController.get({
query: { id, requiredNum: 1, requiredNumArr: [0], disable: 'true' },
body: undefined,
headers: undefined
query: { id, requiredNum: 1, requiredNumArr: [0], disable: 'true' }
})
).resolves.toHaveProperty('body', { id: +id })
expect(val).toBe(+id)
Expand Down
8 changes: 6 additions & 2 deletions servers/all/$server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ type BlobToFile<T extends AspidaMethodParams> = T['reqFormat'] extends FormData
}
: T['reqBody']

type RequestParams<T extends AspidaMethodParams> = {
type RequestParams<T extends AspidaMethodParams> = Pick<{
query: T['query']
body: BlobToFile<T>
headers: T['reqHeaders']
}
}, {
query: Required<T>['query'] extends {} | null ? 'query' : never
body: Required<T>['reqBody'] extends {} | null ? 'body' : never
headers: Required<T>['reqHeaders'] extends {} | null ? 'headers' : never
}['query' | 'body' | 'headers']>

export type ServerMethods<T extends AspidaMethods, U extends ServerValues> = {
[K in keyof T]: (
Expand Down
8 changes: 6 additions & 2 deletions servers/minimum/$server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ type ServerValues = {
user?: any
}

type RequestParams<T extends AspidaMethodParams> = {
type RequestParams<T extends AspidaMethodParams> = Pick<{
query: T['query']
body: T['reqBody']
headers: T['reqHeaders']
}
}, {
query: Required<T>['query'] extends {} | null ? 'query' : never
body: Required<T>['reqBody'] extends {} | null ? 'body' : never
headers: Required<T>['reqHeaders'] extends {} | null ? 'headers' : never
}['query' | 'body' | 'headers']>

export type ServerMethods<T extends AspidaMethods, U extends ServerValues> = {
[K in keyof T]: (
Expand Down
8 changes: 6 additions & 2 deletions servers/noMulter/$server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ type ServerValues = {
user?: any
}

type RequestParams<T extends AspidaMethodParams> = {
type RequestParams<T extends AspidaMethodParams> = Pick<{
query: T['query']
body: T['reqBody']
headers: T['reqHeaders']
}
}, {
query: Required<T>['query'] extends {} | null ? 'query' : never
body: Required<T>['reqBody'] extends {} | null ? 'body' : never
headers: Required<T>['reqHeaders'] extends {} | null ? 'headers' : never
}['query' | 'body' | 'headers']>

export type ServerMethods<T extends AspidaMethods, U extends ServerValues> = {
[K in keyof T]: (
Expand Down
8 changes: 6 additions & 2 deletions servers/noTypedParams/$server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ type BlobToFile<T extends AspidaMethodParams> = T['reqFormat'] extends FormData
}
: T['reqBody']

type RequestParams<T extends AspidaMethodParams> = {
type RequestParams<T extends AspidaMethodParams> = Pick<{
query: T['query']
body: BlobToFile<T>
headers: T['reqHeaders']
}
}, {
query: Required<T>['query'] extends {} | null ? 'query' : never
body: Required<T>['reqBody'] extends {} | null ? 'body' : never
headers: Required<T>['reqHeaders'] extends {} | null ? 'headers' : never
}['query' | 'body' | 'headers']>

export type ServerMethods<T extends AspidaMethods, U extends ServerValues> = {
[K in keyof T]: (
Expand Down
8 changes: 6 additions & 2 deletions servers/noValidator/$server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ type BlobToFile<T extends AspidaMethodParams> = T['reqFormat'] extends FormData
}
: T['reqBody']

type RequestParams<T extends AspidaMethodParams> = {
type RequestParams<T extends AspidaMethodParams> = Pick<{
query: T['query']
body: BlobToFile<T>
headers: T['reqHeaders']
}
}, {
query: Required<T>['query'] extends {} | null ? 'query' : never
body: Required<T>['reqBody'] extends {} | null ? 'body' : never
headers: Required<T>['reqHeaders'] extends {} | null ? 'headers' : never
}['query' | 'body' | 'headers']>

export type ServerMethods<T extends AspidaMethods, U extends ServerValues> = {
[K in keyof T]: (
Expand Down
8 changes: 6 additions & 2 deletions src/buildServerFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,15 @@ type BlobToFile<T extends AspidaMethodParams> = T['reqFormat'] extends FormData
`
: ''
}
type RequestParams<T extends AspidaMethodParams> = {
type RequestParams<T extends AspidaMethodParams> = Pick<{
query: T['query']
body: ${hasMultipart ? 'BlobToFile<T>' : "T['reqBody']"}
headers: T['reqHeaders']
}
}, {
query: Required<T>['query'] extends {} | null ? 'query' : never
body: Required<T>['reqBody'] extends {} | null ? 'body' : never
headers: Required<T>['reqHeaders'] extends {} | null ? 'headers' : never
}['query' | 'body' | 'headers']>
export type ServerMethods<T extends AspidaMethods, U extends ServerValues> = {
[K in keyof T]: (
Expand Down

0 comments on commit 4a810aa

Please sign in to comment.