Browse Source

more typings

tags/v1.0.0-alpha.0
ojizero 1 year ago
parent
commit
e05c6a923b
No account linked to committer's email address
5 changed files with 21 additions and 13 deletions
  1. 3
    3
      src/client.ts
  2. 7
    5
      src/index.ts
  3. 6
    3
      src/method.ts
  4. 4
    1
      src/resource.ts
  5. 1
    1
      test/method.spec.ts

+ 3
- 3
src/client.ts View File

@@ -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 {
// TODO:
}

request (method: string, path: string, payload: {}, options: any) {
// TODO:
request (method: string, path: string, payload: {}, options: any): Promise<any> {
return Promise.resolve() // TODO:
}
}


+ 7
- 5
src/index.ts View File

@@ -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 {
}

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 {
}

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,
}


+ 6
- 3
src/method.ts View File

@@ -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) {
}
}

return async function (...args: any[]) {
return async function (...args: any[]): Promise<any> {
let length = args.length

let query

+ 4
- 1
src/resource.ts View File

@@ -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 {
}
}

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)
}

+ 1
- 1
test/method.spec.ts View File

@@ -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