-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest.ts
More file actions
37 lines (31 loc) · 1.23 KB
/
rest.ts
File metadata and controls
37 lines (31 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { expandableDecorator } from './expandableDecorator'
export interface CorsConfig {
headers?: string[],
methods?: string[],
origin?: string,
credentials?: boolean
}
export const rest = expandableDecorator<{ path: string, methods?: string[], cors?: boolean, corsConfig?: CorsConfig, authenticated?: boolean }>({
name: 'rest',
defaultValues: {
methods: ['get'],
cors: true,
authenticated: false
}
})
export interface IHttpMethod {
(path: string): Function
(config: { path: string, cors?: boolean, corsConfig?: CorsConfig, authenticated?: boolean }): Function
}
export const resolveParam = (p: any, defaults) => {
if (typeof p === 'string') {
return { ...defaults, path: p }
} else {
return { ...defaults, ...p }
}
}
export const httpGet: IHttpMethod = (p) => rest(resolveParam(p, { methods: ['get'] }))
export const httpPost: IHttpMethod = (p) => rest(resolveParam(p, { methods: ['post'] }))
export const httpPut: IHttpMethod = (p) => rest(resolveParam(p, { methods: ['put'] }))
export const httpPatch: IHttpMethod = (p) => rest(resolveParam(p, { methods: ['patch'] }))
export const httpDelete: IHttpMethod = (p) => rest(resolveParam(p, { methods: ['delete'] }))