Move types to types.ts, rename some types
This commit is contained in:
parent
9aba9000b7
commit
1cb41e4b4a
@ -1,3 +1,4 @@
|
|||||||
|
import {FunctionPropertyNames} from './types'
|
||||||
import {NextFunction, Request, Response, Router} from 'express'
|
import {NextFunction, Request, Response, Router} from 'express'
|
||||||
|
|
||||||
export const ERROR_PARSE = {
|
export const ERROR_PARSE = {
|
||||||
@ -34,7 +35,7 @@ export interface ISuccessResponse<T> {
|
|||||||
error: null
|
error: null
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IJsonRpcError<T> {
|
export interface IJSONRPCError<T> {
|
||||||
code: number
|
code: number
|
||||||
message: string
|
message: string
|
||||||
data: T
|
data: T
|
||||||
@ -44,10 +45,10 @@ export interface IErrorResponse<T> {
|
|||||||
jsonrpc: '2.0'
|
jsonrpc: '2.0'
|
||||||
id: number | null
|
id: number | null
|
||||||
result: null
|
result: null
|
||||||
error: IJsonRpcError<T>
|
error: IJSONRPCError<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type IRpcResponse<T> = ISuccessResponse<T> | IErrorResponse<T>
|
export type IRPCResponse<T> = ISuccessResponse<T> | IErrorResponse<T>
|
||||||
|
|
||||||
export function createSuccessResponse<T>(id: number, result: T)
|
export function createSuccessResponse<T>(id: number, result: T)
|
||||||
: ISuccessResponse<T> {
|
: ISuccessResponse<T> {
|
||||||
@ -60,7 +61,7 @@ export function createSuccessResponse<T>(id: number, result: T)
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function createErrorResponse<T>(
|
export function createErrorResponse<T>(
|
||||||
id: number | null, error: IJsonRpcError<T>)
|
id: number | null, error: IJSONRPCError<T>)
|
||||||
: IErrorResponse<T> {
|
: IErrorResponse<T> {
|
||||||
return {
|
return {
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
@ -70,11 +71,6 @@ export function createErrorResponse<T>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type FunctionPropertyNames<T> = {
|
|
||||||
// tslint:disable-next-line
|
|
||||||
[K in keyof T]: T[K] extends Function ? K : never
|
|
||||||
}[keyof T]
|
|
||||||
|
|
||||||
export function pick<T, K extends FunctionPropertyNames<T>>(t: T, keys: K[])
|
export function pick<T, K extends FunctionPropertyNames<T>>(t: T, keys: K[])
|
||||||
: Pick<T, K> {
|
: Pick<T, K> {
|
||||||
return keys.reduce((obj, key) => {
|
return keys.reduce((obj, key) => {
|
||||||
@ -91,16 +87,18 @@ function isPromise(value: any): value is Promise<unknown> {
|
|||||||
typeof value.then === 'function' && typeof value.catch === 'function'
|
typeof value.then === 'function' && typeof value.catch === 'function'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type TGetContext<Context> = (req: Request) => Context
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds middleware for handling JSON RPC requests. Expects JSON middleware to
|
* Adds middleware for handling JSON RPC requests. Expects JSON middleware to
|
||||||
* already be configured.
|
* already be configured.
|
||||||
*/
|
*/
|
||||||
export function jsonrpc<T, F extends FunctionPropertyNames<T>, Ctx>(
|
export function jsonrpc<T, F extends FunctionPropertyNames<T>, Context>(
|
||||||
service: T,
|
service: T,
|
||||||
functions: F[],
|
methods: F[],
|
||||||
getContext: (req: Request) => Ctx,
|
getContext: TGetContext<Context>,
|
||||||
) {
|
) {
|
||||||
const rpcService = pick(service, functions)
|
const rpcService = pick(service, methods)
|
||||||
|
|
||||||
const router = Router()
|
const router = Router()
|
||||||
|
|
||||||
|
|||||||
@ -2,3 +2,4 @@ export * from './express'
|
|||||||
export * from './local'
|
export * from './local'
|
||||||
export * from './redux'
|
export * from './redux'
|
||||||
export * from './remote'
|
export * from './remote'
|
||||||
|
export * from './types'
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import express from 'express'
|
|||||||
import {AddressInfo} from 'net'
|
import {AddressInfo} from 'net'
|
||||||
import {Server} from 'http'
|
import {Server} from 'http'
|
||||||
import {createRemoteClient} from './remote'
|
import {createRemoteClient} from './remote'
|
||||||
import {jsonrpc} from './server'
|
import {jsonrpc} from './express'
|
||||||
import {keys} from 'ts-transformer-keys'
|
import {keys} from 'ts-transformer-keys'
|
||||||
|
|
||||||
describe('remote', () => {
|
describe('remote', () => {
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
import Axios from 'axios'
|
import Axios from 'axios'
|
||||||
import {TAsyncified} from './types'
|
import {FunctionPropertyNames, TAsyncified} from './types'
|
||||||
|
|
||||||
export function createRemoteClient<T>(
|
export function createRemoteClient<T>(
|
||||||
baseUrl: string,
|
baseUrl: string,
|
||||||
url: string,
|
url: string,
|
||||||
methods: Array<keyof T>,
|
methods: Array<FunctionPropertyNames<T>>,
|
||||||
) {
|
) {
|
||||||
const axios = Axios.create({
|
const axios = Axios.create({
|
||||||
baseURL: baseUrl,
|
baseURL: baseUrl,
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
import {IPendingAction, IResolvedAction, IRejectedAction} from '@rondo/client'
|
||||||
|
|
||||||
type ArgumentTypes<T> =
|
type ArgumentTypes<T> =
|
||||||
T extends (...args: infer U) => infer R ? U : never
|
T extends (...args: infer U) => infer R ? U : never
|
||||||
type RetType<T> = T extends (...args: any[]) => infer R ? R : never
|
type RetType<T> = T extends (...args: any[]) => infer R ? R : never
|
||||||
@ -7,7 +9,9 @@ type RetProm<T> = T extends Promise<any> ? T : Promise<T>
|
|||||||
type PromisifyReturnType<T> = (...a: ArgumentTypes<T>) =>
|
type PromisifyReturnType<T> = (...a: ArgumentTypes<T>) =>
|
||||||
RetProm<UnwrapHOC<RetType<T>>>
|
RetProm<UnwrapHOC<RetType<T>>>
|
||||||
|
|
||||||
import {IPendingAction, IResolvedAction, IRejectedAction} from '@rondo/client'
|
export type FunctionPropertyNames<T> = {
|
||||||
|
[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never
|
||||||
|
}[keyof T]
|
||||||
|
|
||||||
export type TAsyncified<T> = {
|
export type TAsyncified<T> = {
|
||||||
[K in keyof T]: PromisifyReturnType<T[K]>
|
[K in keyof T]: PromisifyReturnType<T[K]>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user