Browse Source

more typings

modified-method-api
ojizero 4 years ago
parent
commit
e05c6a923b
No known key found for this signature in database
GPG Key ID: FEBB7097845B0C7E
  1. 6
      src/client.ts
  2. 12
      src/index.ts
  3. 9
      src/method.ts
  4. 5
      src/resource.ts
  5. 2
      test/method.spec.ts

6
src/client.ts

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
export interface Client {
request (method: string, path: string, payload: {}, options: any): void // TODO:
request (method: string, path: string, payload: {}, options: any): Promise<any> // TODO:
}
export class PortalClient implements Client {
@ -7,8 +7,8 @@ export class PortalClient implements Client { @@ -7,8 +7,8 @@ export class PortalClient implements Client {
// TODO:
}
request (method: string, path: string, payload: {}, options: any) {
// TODO:
request (method: string, path: string, payload: {}, options: any): Promise<any> {
return Promise.resolve() // TODO:
}
}

12
src/index.ts

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
import Client from './client'
// import method from './method'
// import Resource from './resource'
import method, { MethodFactory } from './method'
import resrouceGenerator, { ResourceFactory } from './resource'
import { URL } from 'url' // TODO: declare it instead of importing it
@ -32,8 +32,8 @@ export interface Config { @@ -32,8 +32,8 @@ export interface Config {
}
export interface Portal {
// route: method, // TODO: function taking some spec
// resource: Resource, // TODO: function taking some spec
route: MethodFactory,
resource: ResourceFactory,
_client: Client,
}
@ -48,9 +48,11 @@ export interface Response { @@ -48,9 +48,11 @@ export interface Response {
}
export function createBaseClient (config: Config): Portal {
const client = new Client()
const client = new Client() // TODO:
const portal: Portal = {
route: method(client),
resource: resrouceGenerator(client),
_client: client,
}

9
src/method.ts

@ -21,8 +21,11 @@ export interface MethodSpec { @@ -21,8 +21,11 @@ export interface MethodSpec {
// strict?: boolean,
}
export function method (client: Client) {
return function methodGenerator (spec: MethodSpec) {
export type RouteFunction = (...args: any[]) => Promise<any>
export type MethodFactory = (spec: MethodSpec) => RouteFunction
export function method (client: Client): MethodFactory {
return function methodGenerator (spec: MethodSpec): RouteFunction {
const {
path,
method = 'GET',
@ -40,7 +43,7 @@ export function method (client: Client) { @@ -40,7 +43,7 @@ export function method (client: Client) {
}
}
return async function (...args: any[]) {
return async function (...args: any[]): Promise<any> {
let length = args.length
let query

5
src/resource.ts

@ -24,6 +24,9 @@ const defaultBaseSpecs: { [k: string]: MethodSpec } = { @@ -24,6 +24,9 @@ const defaultBaseSpecs: { [k: string]: MethodSpec } = {
},
}
export type ResourceFactory =
(baseRoute: string, enabledRoutes?: string[], extraMethods?: { [k:string]: any }) => Resource
export class Resource {
client: Client
baseRoute: string
@ -115,7 +118,7 @@ export class Resource { @@ -115,7 +118,7 @@ export class Resource {
}
}
export default function resrouceGenerator (client: Client) {
export default function resrouceGenerator (client: Client): ResourceFactory {
return function (baseRoute: string, enabledRoutes?: string[], extraMethods?: { [k:string]: any }): Resource {
return new Resource(client, baseRoute, enabledRoutes, extraMethods)
}

2
test/method.spec.ts

@ -50,7 +50,7 @@ describe('Method', async () => { @@ -50,7 +50,7 @@ describe('Method', async () => {
let methodFunction
before(() => {
client = { request (method, path, options) {} }
client = { request (method, path, options) { return Promise.resolve() } }
})
it('accepts client and returns generator function', async () => {

Loading…
Cancel
Save