Rename jsonrpc types
This commit is contained in:
parent
16400d01c3
commit
195d7226e8
@ -1,4 +1,4 @@
|
|||||||
import { TReduxed } from '@rondo.dev/jsonrpc'
|
import { RPCActions } from '@rondo.dev/jsonrpc'
|
||||||
import { keys } from 'ts-transformer-keys'
|
import { keys } from 'ts-transformer-keys'
|
||||||
import { Team } from '../entities'
|
import { Team } from '../entities'
|
||||||
import { ITeamAddUserParams } from './ITeamAddUserParams'
|
import { ITeamAddUserParams } from './ITeamAddUserParams'
|
||||||
@ -27,4 +27,4 @@ export interface ITeamService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const TeamServiceMethods = keys<ITeamService>()
|
export const TeamServiceMethods = keys<ITeamService>()
|
||||||
export type TeamActions = TReduxed<ITeamService, 'teamService'>
|
export type TeamActions = RPCActions<ITeamService, 'teamService'>
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { TReduxed } from '@rondo.dev/jsonrpc'
|
import { RPCActions } from '@rondo.dev/jsonrpc'
|
||||||
import { keys } from 'ts-transformer-keys'
|
import { keys } from 'ts-transformer-keys'
|
||||||
import { IUser } from './IUser'
|
import { IUser } from './IUser'
|
||||||
|
|
||||||
@ -9,4 +9,4 @@ export interface IUserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const UserServiceMethods = keys<IUserService>()
|
export const UserServiceMethods = keys<IUserService>()
|
||||||
export type UserActions = TReduxed<IUserService, 'userService'>
|
export type UserActions = RPCActions<IUserService, 'userService'>
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import * as util from './bulk'
|
import * as util from './bulk'
|
||||||
import express from 'express'
|
import express from 'express'
|
||||||
import {Contextual} from './types'
|
import {WithContext} from './types'
|
||||||
import {jsonrpc} from './express'
|
import {jsonrpc} from './express'
|
||||||
import {noopLogger} from './test-utils'
|
import {noopLogger} from './test-utils'
|
||||||
import {createClient} from './supertest'
|
import {createClient} from './supertest'
|
||||||
@ -21,13 +21,13 @@ describe('util', () => {
|
|||||||
userId: number
|
userId: number
|
||||||
}
|
}
|
||||||
|
|
||||||
class Service1 implements Contextual<IS1, IContext> {
|
class Service1 implements WithContext<IS1, IContext> {
|
||||||
add(cx: IContext, a: number, b: number) {
|
add(cx: IContext, a: number, b: number) {
|
||||||
return a + b + cx.userId
|
return a + b + cx.userId
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Service2 implements Contextual<IS2, IContext> {
|
class Service2 implements WithContext<IS2, IContext> {
|
||||||
mul(cx: IContext, a: number, b: number) {
|
mul(cx: IContext, a: number, b: number) {
|
||||||
return a * b + cx.userId
|
return a * b + cx.userId
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import {IJSONRPCReturnType} from './express'
|
import {IJSONRPCReturnType} from './express'
|
||||||
import {Contextual, TAsyncified, TReduxed} from './types'
|
import {WithContext, RPCClient, RPCActions} from './types'
|
||||||
import {createActions} from './redux'
|
import {createActions} from './redux'
|
||||||
import {createLocalClient, LocalClient} from './local'
|
import {createLocalClient, LocalClient} from './local'
|
||||||
|
|
||||||
@ -8,11 +8,11 @@ function keys<T>(obj: T): Array<keyof T & string> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type BulkServices<T, Cx> = {
|
export type BulkServices<T, Cx> = {
|
||||||
[K in keyof T & string]: Contextual<T[K], Cx>
|
[K in keyof T & string]: WithContext<T[K], Cx>
|
||||||
}
|
}
|
||||||
export type BulkLocalClient<T> = {[K in keyof T & string]: LocalClient<T[K]>}
|
export type BulkLocalClient<T> = {[K in keyof T & string]: LocalClient<T[K]>}
|
||||||
export type BulkActions<T> = {[K in keyof T & string]: TReduxed<T[K], K>}
|
export type BulkActions<T> = {[K in keyof T & string]: RPCActions<T[K], K>}
|
||||||
export type BulkClient<T> = {[K in keyof T & string]: TAsyncified<T[K]>}
|
export type BulkClient<T> = {[K in keyof T & string]: RPCClient<T[K]>}
|
||||||
|
|
||||||
function bulkCreate<T, R>(
|
function bulkCreate<T, R>(
|
||||||
src: T,
|
src: T,
|
||||||
@ -25,14 +25,14 @@ function bulkCreate<T, R>(
|
|||||||
}, {} as any)
|
}, {} as any)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function bulkCreateLocalClient<Cx, T extends Contextual<{}, Cx>>(
|
export function bulkCreateLocalClient<Cx, T extends WithContext<{}, Cx>>(
|
||||||
src: T,
|
src: T,
|
||||||
context: Cx,
|
context: Cx,
|
||||||
): BulkLocalClient<T> {
|
): BulkLocalClient<T> {
|
||||||
return bulkCreate(src, (key, value) => createLocalClient(value, context))
|
return bulkCreate(src, (key, value) => createLocalClient(value, context))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function bulkCreateActions<T extends Record<string, TAsyncified<any>>>(
|
export function bulkCreateActions<T extends Record<string, RPCClient<any>>>(
|
||||||
src: T,
|
src: T,
|
||||||
): BulkActions<T> {
|
): BulkActions<T> {
|
||||||
return bulkCreate(src, (key, value) => createActions(value, key))
|
return bulkCreate(src, (key, value) => createActions(value, key))
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { FunctionPropertyNames, TAsyncified } from './types'
|
import { FunctionPropertyNames, RPCClient } from './types'
|
||||||
|
|
||||||
export type TMocked<T> = {
|
export type TMocked<T> = {
|
||||||
[K in keyof T]:
|
[K in keyof T]:
|
||||||
@ -20,7 +20,7 @@ export type TMocked<T> = {
|
|||||||
*/
|
*/
|
||||||
export default function createClientMock<T extends object>(
|
export default function createClientMock<T extends object>(
|
||||||
methods: Array<FunctionPropertyNames<T>>,
|
methods: Array<FunctionPropertyNames<T>>,
|
||||||
): [TAsyncified<T>, TMocked<TAsyncified<T>>] {
|
): [RPCClient<T>, TMocked<RPCClient<T>>] {
|
||||||
const client = methods
|
const client = methods
|
||||||
.reduce((obj, prop) => {
|
.reduce((obj, prop) => {
|
||||||
obj[prop] = jest.fn()
|
obj[prop] = jest.fn()
|
||||||
@ -28,7 +28,7 @@ export default function createClientMock<T extends object>(
|
|||||||
}, {} as any)
|
}, {} as any)
|
||||||
|
|
||||||
return [
|
return [
|
||||||
client as TAsyncified<T>,
|
client as RPCClient<T>,
|
||||||
client as TMocked<TAsyncified<T>>,
|
client as TMocked<RPCClient<T>>,
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import {createClient} from './supertest'
|
|||||||
import {ensure} from './ensure'
|
import {ensure} from './ensure'
|
||||||
import {jsonrpc} from './express'
|
import {jsonrpc} from './express'
|
||||||
import {noopLogger} from './test-utils'
|
import {noopLogger} from './test-utils'
|
||||||
import {Contextual} from './types'
|
import {WithContext} from './types'
|
||||||
|
|
||||||
describe('jsonrpc', () => {
|
describe('jsonrpc', () => {
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ describe('jsonrpc', () => {
|
|||||||
|
|
||||||
const ensureLoggedIn = ensure<IContext>(c => !!c.userId)
|
const ensureLoggedIn = ensure<IContext>(c => !!c.userId)
|
||||||
|
|
||||||
class Service implements Contextual<IService, IContext> {
|
class Service implements WithContext<IService, IContext> {
|
||||||
constructor(readonly time: number) {}
|
constructor(readonly time: number) {}
|
||||||
add(context: IContext, a: number, b: number) {
|
add(context: IContext, a: number, b: number) {
|
||||||
return a + b
|
return a + b
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import {createLocalClient} from './local'
|
import {createLocalClient} from './local'
|
||||||
import {keys} from 'ts-transformer-keys'
|
import {keys} from 'ts-transformer-keys'
|
||||||
import {Contextual, ReverseContextual, TAsyncified} from './types'
|
import {WithContext, WithoutContext, RPCClient} from './types'
|
||||||
|
|
||||||
describe('local', () => {
|
describe('local', () => {
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ describe('local', () => {
|
|||||||
userId: 1000
|
userId: 1000
|
||||||
}
|
}
|
||||||
|
|
||||||
class Service implements Contextual<IService, IContext> {
|
class Service implements WithContext<IService, IContext> {
|
||||||
add(cx: IContext, a: number, b: number) {
|
add(cx: IContext, a: number, b: number) {
|
||||||
return a + b
|
return a + b
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
import {TAsyncified, Contextual, ReverseContextual} from './types'
|
import {RPCClient, WithContext, WithoutContext} from './types'
|
||||||
import {Request} from 'express'
|
import {Request} from 'express'
|
||||||
import {TGetContext} from './express'
|
import {TGetContext} from './express'
|
||||||
import {getAllMethods} from './jsonrpc'
|
import {getAllMethods} from './jsonrpc'
|
||||||
|
|
||||||
export type LocalClient<T> = TAsyncified<ReverseContextual<T>>
|
export type LocalClient<T> = RPCClient<WithoutContext<T>>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a local client for a specific service instance. The actual service
|
* Creates a local client for a specific service instance. The actual service
|
||||||
@ -11,7 +11,7 @@ export type LocalClient<T> = TAsyncified<ReverseContextual<T>>
|
|||||||
* on the client- and server-side.
|
* on the client- and server-side.
|
||||||
*
|
*
|
||||||
* The service argument is expected to be a class implementing the
|
* The service argument is expected to be a class implementing the
|
||||||
* Contextual<Service, Context> type. The first (context) argument will be
|
* WithContext<Service, Context> type. The first (context) argument will be
|
||||||
* automatically removed from all methods in the service, and the supplied
|
* automatically removed from all methods in the service, and the supplied
|
||||||
* context argument will be used instead.
|
* context argument will be used instead.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import bodyParser from 'body-parser'
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import {AddressInfo} from 'net'
|
import {AddressInfo} from 'net'
|
||||||
import {Server} from 'http'
|
import {Server} from 'http'
|
||||||
import {Contextual, TPendingActions, TAllActions} from './types'
|
import {WithContext, TPendingActions, TAllActions} from './types'
|
||||||
import {combineReducers} from 'redux'
|
import {combineReducers} from 'redux'
|
||||||
import {createActions, createReducer} from './redux'
|
import {createActions, createReducer} from './redux'
|
||||||
import {createRemoteClient} from './remote'
|
import {createRemoteClient} from './remote'
|
||||||
@ -30,7 +30,7 @@ describe('createActions', () => {
|
|||||||
userId: number
|
userId: number
|
||||||
}
|
}
|
||||||
|
|
||||||
class Service implements Contextual<IService, IContext> {
|
class Service implements WithContext<IService, IContext> {
|
||||||
add(cx: IContext, a: number, b: number) {
|
add(cx: IContext, a: number, b: number) {
|
||||||
return a + b
|
return a + b
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,14 @@
|
|||||||
import {
|
import {
|
||||||
IReduxed,
|
IRPCActions,
|
||||||
TAsyncified,
|
RPCClient,
|
||||||
TReduxed,
|
RPCActions,
|
||||||
TResolvedActions,
|
TResolvedActions,
|
||||||
TAllActions,
|
TAllActions,
|
||||||
TReduxHandlers,
|
RPCReduxHandlers,
|
||||||
} from './types'
|
} from './types'
|
||||||
|
|
||||||
export function createActions<T, ActionType extends string>(
|
export function createActions<T, ActionType extends string>(
|
||||||
client: TAsyncified<T>,
|
client: RPCClient<T>,
|
||||||
type: ActionType,
|
type: ActionType,
|
||||||
) {
|
) {
|
||||||
const service = Object.keys(client).reduce((obj, method: any) => {
|
const service = Object.keys(client).reduce((obj, method: any) => {
|
||||||
@ -24,7 +24,7 @@ export function createActions<T, ActionType extends string>(
|
|||||||
return obj
|
return obj
|
||||||
}, {} as any)
|
}, {} as any)
|
||||||
|
|
||||||
return service as TReduxed<T, ActionType>
|
return service as RPCActions<T, ActionType>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IState {
|
export interface IState {
|
||||||
@ -38,7 +38,7 @@ export function createReducer<ActionType extends string, State extends IState>(
|
|||||||
) {
|
) {
|
||||||
|
|
||||||
const self = {
|
const self = {
|
||||||
withHandler<R extends IReduxed<ActionType>>(
|
withHandler<R extends IRPCActions<ActionType>>(
|
||||||
handleAction: (state: State, action: TResolvedActions<R>) => State,
|
handleAction: (state: State, action: TResolvedActions<R>) => State,
|
||||||
): (state: State | undefined, action: TAllActions<R>) => State {
|
): (state: State | undefined, action: TAllActions<R>) => State {
|
||||||
return (state: State = defaultState, action: TAllActions<R>): State => {
|
return (state: State = defaultState, action: TAllActions<R>): State => {
|
||||||
@ -66,8 +66,8 @@ export function createReducer<ActionType extends string, State extends IState>(
|
|||||||
}, action)
|
}, action)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
withMapping<R extends IReduxed<ActionType>>(
|
withMapping<R extends IRPCActions<ActionType>>(
|
||||||
handlers: TReduxHandlers<R, State>,
|
handlers: RPCReduxHandlers<R, State>,
|
||||||
) {
|
) {
|
||||||
return self.withHandler<R>((state, action) => {
|
return self.withHandler<R>((state, action) => {
|
||||||
if (action.method in handlers) {
|
if (action.method in handlers) {
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import {createRemoteClient} from './remote'
|
|||||||
import {jsonrpc} from './express'
|
import {jsonrpc} from './express'
|
||||||
import {keys} from 'ts-transformer-keys'
|
import {keys} from 'ts-transformer-keys'
|
||||||
import {noopLogger} from './test-utils'
|
import {noopLogger} from './test-utils'
|
||||||
import {Contextual} from './types'
|
import {WithContext} from './types'
|
||||||
|
|
||||||
describe('remote', () => {
|
describe('remote', () => {
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ describe('remote', () => {
|
|||||||
}
|
}
|
||||||
const IServiceKeys = keys<IService>()
|
const IServiceKeys = keys<IService>()
|
||||||
|
|
||||||
class Service implements Contextual<IService, {}> {
|
class Service implements WithContext<IService, {}> {
|
||||||
add(ctx: {}, a: number, b: number) {
|
add(ctx: {}, a: number, b: number) {
|
||||||
return a + b
|
return a + b
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import Axios from 'axios'
|
import Axios from 'axios'
|
||||||
import {FunctionPropertyNames, TAsyncified} from './types'
|
import {FunctionPropertyNames, RPCClient} from './types'
|
||||||
import {IDEMPOTENT_METHOD_REGEX} from './idempotent'
|
import {IDEMPOTENT_METHOD_REGEX} from './idempotent'
|
||||||
|
|
||||||
export type TRequestIdGenerator<T extends string | number> = () => T
|
export type TRequestIdGenerator<T extends string | number> = () => T
|
||||||
@ -51,5 +51,5 @@ export function createRemoteClient<T>(
|
|||||||
return obj
|
return obj
|
||||||
}, {} as any)
|
}, {} as any)
|
||||||
|
|
||||||
return service as TAsyncified<T>
|
return service as RPCClient<T>
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import request from 'supertest'
|
import request from 'supertest'
|
||||||
import {Application} from 'express'
|
import {Application} from 'express'
|
||||||
import {TAsyncified} from './types'
|
import {RPCClient} from './types'
|
||||||
|
|
||||||
export function createClient<T>(app: Application, path: string,
|
export function createClient<T>(app: Application, path: string,
|
||||||
) {
|
) {
|
||||||
@ -25,5 +25,5 @@ export function createClient<T>(app: Application, path: string,
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
return proxy as TAsyncified<T>
|
return proxy as RPCClient<T>
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,14 +12,14 @@ type PromisifyReturnType<T> = (...a: ArgumentTypes<T>) =>
|
|||||||
* Helps implement a service from a service definiton that has a context as a
|
* Helps implement a service from a service definiton that has a context as a
|
||||||
* last argument.
|
* last argument.
|
||||||
*/
|
*/
|
||||||
export type Contextual<T, Cx> = {
|
export type WithContext<T, Cx> = {
|
||||||
[K in keyof T]:
|
[K in keyof T]:
|
||||||
T[K] extends (...args: infer A) => infer R
|
T[K] extends (...args: infer A) => infer R
|
||||||
? (cx: Cx, ...args: A) => R :
|
? (cx: Cx, ...args: A) => R :
|
||||||
never
|
never
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ReverseContextual<T> = {
|
export type WithoutContext<T> = {
|
||||||
[K in keyof T]:
|
[K in keyof T]:
|
||||||
T[K] extends (cx: any, ...args: infer A) => infer R
|
T[K] extends (cx: any, ...args: infer A) => infer R
|
||||||
? (...args: A) => R :
|
? (...args: A) => R :
|
||||||
@ -34,20 +34,20 @@ export type FunctionPropertyNames<T> = {
|
|||||||
: never
|
: never
|
||||||
}[keyof T]
|
}[keyof T]
|
||||||
|
|
||||||
export type TAsyncified<T> = {
|
export type RPCClient<T> = {
|
||||||
[K in keyof T]: PromisifyReturnType<T[K]>
|
[K in keyof T]: PromisifyReturnType<T[K]>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IReduxed<ActionType extends string> {
|
export interface IRPCActions<ActionType extends string> {
|
||||||
[key: string]: (...a: any[]) => IRPCPendingAction<any, ActionType, typeof key>
|
[key: string]: (...a: any[]) => IRPCPendingAction<any, ActionType, typeof key>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TReduxed<T, ActionType extends string> = {
|
export type RPCActions<T, ActionType extends string> = {
|
||||||
[K in keyof T]: (...a: ArgumentTypes<T[K]>) =>
|
[K in keyof T]: (...a: ArgumentTypes<T[K]>) =>
|
||||||
IRPCPendingAction<UnwrapPromise<RetType<T[K]>>, ActionType, K>
|
IRPCPendingAction<UnwrapPromise<RetType<T[K]>>, ActionType, K>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TReduxHandlers<T, State> = {
|
export type RPCReduxHandlers<T, State> = {
|
||||||
[K in keyof T]: (
|
[K in keyof T]: (
|
||||||
state: State,
|
state: State,
|
||||||
action: TResolved<TPending<RetType<T[K]>>>,
|
action: TResolved<TPending<RetType<T[K]>>>,
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
import { IContext } from '@rondo.dev/common'
|
import { IContext } from '@rondo.dev/common'
|
||||||
import { Contextual, ensure } from '@rondo.dev/jsonrpc'
|
import { WithContext, ensure } from '@rondo.dev/jsonrpc'
|
||||||
|
|
||||||
export { IContext }
|
export { IContext }
|
||||||
export type RPC<Service> = Contextual<Service, IContext>
|
export type RPC<Service> = WithContext<Service, IContext>
|
||||||
|
|
||||||
export const ensureLoggedIn = ensure<IContext>(
|
export const ensureLoggedIn = ensure<IContext>(
|
||||||
c => !!c.user && !!c.user.id,
|
c => !!c.user && !!c.user.id,
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import {Role} from '../entities/Role'
|
|||||||
import {CORRELATION_ID} from '../middleware'
|
import {CORRELATION_ID} from '../middleware'
|
||||||
import shortid from 'shortid'
|
import shortid from 'shortid'
|
||||||
import { AddressInfo } from 'net'
|
import { AddressInfo } from 'net'
|
||||||
import { createRemoteClient, FunctionPropertyNames, TAsyncified } from '@rondo.dev/jsonrpc'
|
import { createRemoteClient, FunctionPropertyNames, RPCClient } from '@rondo.dev/jsonrpc'
|
||||||
import {Server} from 'http'
|
import {Server} from 'http'
|
||||||
import { IAppServer } from '../application/IAppServer'
|
import { IAppServer } from '../application/IAppServer'
|
||||||
|
|
||||||
@ -198,7 +198,7 @@ export class TestUtils<T extends IRoutes> {
|
|||||||
return obj
|
return obj
|
||||||
}, {} as any)
|
}, {} as any)
|
||||||
|
|
||||||
return service as TAsyncified<S>
|
return service as RPCClient<S>
|
||||||
}
|
}
|
||||||
|
|
||||||
private getCookies(setCookiesString: string[]): string {
|
private getCookies(setCookiesString: string[]): string {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user