Skip to content

Commit

Permalink
feat: Simplify createResouceRouter and add option to set subrouter co…
Browse files Browse the repository at this point in the history
…nfig

Signed-off-by: Jorgen Ader <[email protected]>
  • Loading branch information
jorgenader committed Nov 26, 2023
1 parent 82fe319 commit 92019c0
Show file tree
Hide file tree
Showing 6 changed files with 394 additions and 52 deletions.
35 changes: 16 additions & 19 deletions packages/core/src/router-builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,30 @@ import {
Kwargs,
ObjectMap,
ResourceInterface,
RouteConfig,
RouteMap,
RouterDefinition,
} from '../types';

import {
CreateResourceFactory,
ResourceClassConstructor,
ResourceConstructorObject,
ResourceTuple,
RouterBuilderInterface,
} from './types';
import { isResourceConstructorObject, isResourceTuple } from './utils';
import {
createResource,
isResourceConstructorObject,
isResourceTuple,
} from './utils';

export class RouterBuilder<
Klass extends Resource<any, any, any, any>,
InstanceKlass extends Resource<any, any, any, any> = Klass
> implements RouterBuilderInterface
export class RouterBuilder<Klass extends Resource<any, any, any, any>>
implements RouterBuilderInterface<Klass>
{
private readonly resourceKlass: ResourceClassConstructor<Klass>;

private readonly createResourceFactory: CreateResourceFactory<InstanceKlass>;
public readonly resourceKlass: ResourceClassConstructor<Klass>;

constructor(
resourceKlass: ResourceClassConstructor<Klass>,
createResourceFactory: CreateResourceFactory<InstanceKlass>
) {
constructor(resourceKlass: ResourceClassConstructor<Klass>) {
this.resourceKlass = resourceKlass;
this.createResourceFactory = createResourceFactory;
}

resource<
Expand All @@ -45,7 +41,7 @@ export class RouterBuilder<
endpointConfig: ResourceTuple | ResourceConstructorObject | string
): ResourceInterface<Params, TFetchResponse, TPostPayload, TPostResponse> {
if (isString(endpointConfig)) {
return this.createResourceFactory(
return createResource(
this.resourceKlass,
endpointConfig,
null
Expand All @@ -60,7 +56,7 @@ export class RouterBuilder<
if (isResourceTuple(endpointConfig)) {
const [apiEndpoint, resourceConfig] =
endpointConfig as ResourceTuple;
return this.createResourceFactory(
return createResource(
this.resourceKlass,
apiEndpoint,
resourceConfig
Expand All @@ -75,7 +71,7 @@ export class RouterBuilder<
if (isResourceConstructorObject(endpointConfig)) {
const { apiEndpoint, ...resourceConfig } = endpointConfig;

return this.createResourceFactory(
return createResource(
this.resourceKlass,
apiEndpoint,
resourceConfig
Expand All @@ -91,10 +87,11 @@ export class RouterBuilder<
}

router<TRouteMap extends RouteMap>(
builder: (build: this) => TRouteMap
builder: (build: this) => TRouteMap,
config: RouteConfig | null = null
): RouterDefinition<TRouteMap> {
const definition = builder(this);

return new Router(definition) as Router & TRouteMap;
return new Router(definition, config) as Router & TRouteMap;
}
}
22 changes: 5 additions & 17 deletions packages/core/src/router-builder/createResourceRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,16 @@ import { RouteMap, RouterInterface } from '../types';

import { RouterBuilder } from './builder';
import { CreateResourceRouterOptions } from './types';
import { createResource } from './utils';

export function createResourceRouter<
Klass extends Resource<any, any, any, any>,
Definitions extends RouteMap,
InstanceKlass extends Resource<any, any, any, any> = Klass
Definitions extends RouteMap
>(
options: CreateResourceRouterOptions<Klass, Definitions, InstanceKlass>
options: CreateResourceRouterOptions<Klass, Definitions>
): RouterInterface & Definitions {
const {
resource,
createResourceFactory = createResource as Exclude<
typeof options['createResourceFactory'],
undefined
>,
routerBuilder,
} = options;
const { config, resource, routerBuilder } = options;

const builder = new RouterBuilder<Klass, InstanceKlass>(
resource,
createResourceFactory
);
const builder = new RouterBuilder<Klass>(resource);

return new Router(routerBuilder(builder)) as Router & Definitions;
return new Router(routerBuilder(builder), config) as Router & Definitions;
}
19 changes: 9 additions & 10 deletions packages/core/src/router-builder/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export type CreateResourceFactory<
options?: ObjectMap
) => InstanceKlass;

export interface RouterBuilderInterface {
export interface RouterBuilderInterface<
Klass extends Resource<any, any, any, any>
> {
resourceKlass: ResourceClassConstructor<Klass>;

resource<
Params extends Kwargs | null = Kwargs,
TFetchResponse = any,
Expand All @@ -43,25 +47,20 @@ export interface RouterBuilderInterface {
): ResourceInterface<Params, TFetchResponse, TPostPayload, TPostResponse>;

router<TRouteMap extends RouteMap>(
builder: (build: this) => TRouteMap
builder: (build: this) => TRouteMap,
config?: RouteConfig | null
): RouterInterface & TRouteMap;
}

export interface CreateResourceRouterOptions<
Klass extends Resource<any, any, any, any>,
Definitions extends RouteMap,
InstanceKlass extends Resource<any, any, any, any> = Klass
Definitions extends RouteMap
> {
/**
* The resource class to use when creating resources
*/
resource: ResourceClassConstructor<Klass>;

/**
* Helper function used to create resource instances
*/
createResourceFactory?: CreateResourceFactory<InstanceKlass>;

/**
* Router configuration
*/
Expand All @@ -71,5 +70,5 @@ export interface CreateResourceRouterOptions<
* Build router definition
* @param build
*/
routerBuilder: (build: RouterBuilderInterface) => Definitions;
routerBuilder: (build: RouterBuilderInterface<Klass>) => Definitions;
}
5 changes: 3 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ export interface ResourceInterface<

config(requestConfig?: RequestConfig): ConfigType;

getHeaders(requestConfig?: RequestConfig): Record<string, string | null>;
getCookies(requestConfig?: RequestConfig): Record<string, string | null>;

fetch<TResponse = TFetchResponse, TParams extends Params = Params>(
kwargs?: TParams | null,
query?: Query | null,
Expand Down Expand Up @@ -403,8 +406,6 @@ export interface ResourceInterface<
urlParams?: TParams | null,
requestConfig?: RequestConfig | null
): string;

[key: string]: any;
}

export abstract class ResourceErrorInterface {
Expand Down
Loading

0 comments on commit 92019c0

Please sign in to comment.