Skip to content

Commit

Permalink
Repository generator supports API url prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
gius committed Nov 9, 2021
1 parent 50dd8c5 commit 6ec9e7b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 23 deletions.
18 changes: 11 additions & 7 deletions packages/datascreens/src/filteredList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class FilteredList<
}

constructor(
public onLoadData: (filter: TFilter, paging: IPagingFilter) => Promise<PagedQueryResult<TEntity>>,
public onLoadData: (filter: TFilter, paging: IPagingFilter) => Promise<PagedQueryResult<TEntity> | void>,
private initFilter: () => TFilter = () => ({} as TFilter),
private defaultPagingFilter: () => IPagingFilter = () => ({
limit: FilteredList.defaultPageSize,
Expand Down Expand Up @@ -69,18 +69,22 @@ export default class FilteredList<
const paging = this.defaultPagingFilter();
const data = await this.onLoadData(appliedFilter, paging);

runInAction(() => {
this.appliedFilterValue = appliedFilter;
this.setData(data);
});
if (data) {
runInAction(() => {
this.appliedFilterValue = appliedFilter;
this.setData(data);
});
}

return true;
return !!data;
}

@bound
async applyPaging() {
const data = await this.onLoadData(this.appliedFilter, this.pagingFilter);
this.setData(data);
if (data) {
this.setData(data);
}
}

protected cloneFilter(filter: TFilter): TFilter {
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node
// #!/usr/bin/env node
/* eslint-disable sonarjs/no-duplicate-string */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
Expand Down
1 change: 1 addition & 0 deletions packages/generator/src/openapi/defaultConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"validations": {
"readOnly": false
},
"endpointUrlPrefix": "/api/",
"templates": {
"enumEntity": "@enumEntity.hbs",
"enumEntityFile": "@enumEntityFile.hbs",
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/src/openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class OpenApiGenerator extends GeneratorBase<IGeneratorParams, IC
}

const modelProcessor = new ModelProcessor();
const { types, endpoints } = await modelProcessor.process(this.config.api);
const { types, endpoints } = await modelProcessor.process(this.config.api, this.config);

const nameFormatter = new NameFormatter();
const observableFormatter = new ObservableFormatter(this.config.observable);
Expand Down
9 changes: 5 additions & 4 deletions packages/generator/src/openapi/modelProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,32 @@ import type ApiModel from "./models/apiModel";
import { isOpenAPIv2, isOpenAPIv3 } from "./parsers/helpers";
import OpenApi2Parser from "./parsers/openApi2Parser";
import OpenApi3Parser from "./parsers/openApi3Parser";
import type { IApiParserConfig } from "./types";

export default class ModelProcessor {
async process(apiPath: string) {
async process(apiPath: string, parserConfig?: IApiParserConfig) {
const progress = createProgressBar("Analysing");
progress.start(2, 0);

const api = await SwaggerParser.parse(apiPath);
progress.increment();

const model = this.parseModel(api);
const model = this.parseModel(api, parserConfig);
progress.increment();
progress.stop();

return model;
}

private parseModel(api: OpenAPI.Document): ApiModel {
private parseModel(api: OpenAPI.Document, config?: IApiParserConfig): ApiModel {
if (isOpenAPIv2(api)) {
const parser = new OpenApi2Parser(api);
parser.parse();
return parser;
}

if (isOpenAPIv3(api)) {
const parser = new OpenApi3Parser(api);
const parser = new OpenApi3Parser(api, config);
parser.parse();
return parser;
}
Expand Down
6 changes: 4 additions & 2 deletions packages/generator/src/openapi/parsers/openApi3Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import ObjectEntity from "../models/objectEntity";
import Restriction from "../models/restriction";
import TypeReference from "../models/typeReference";
import UnionEntity from "../models/unionEntity";
import type { IApiParserConfig } from "../types";
import { isV3ReferenceObject, isV3SchemaObject } from "./helpers";

export default class OpenApi3Parser implements ApiModel {
types = new Map<string, TypeReference>();
endpoints: Endpoint[];

constructor(private apiDocument: OpenAPIV3.Document) {}
constructor(private apiDocument: OpenAPIV3.Document, private config?: IApiParserConfig) {}

parse() {
if (this.apiDocument.components?.schemas) {
Expand Down Expand Up @@ -210,7 +211,8 @@ export default class OpenApi3Parser implements ApiModel {
}

private parseEndpoint({ path, method, action }: { path: string; method: string; action: OpenAPIV3.OperationObject }) {
const name = action.operationId ?? camelCase(method + path);
path = this.config?.endpointUrlPrefix ? path.replace(this.config.endpointUrlPrefix, "") : path;
const name = action.operationId ?? camelCase(method + "-" + path.replace(/\{\D*?\}/, "")); // the dash makes sure first path word starts with upper case

const endpoint = new Endpoint(name);
endpoint.path = path;
Expand Down
5 changes: 5 additions & 0 deletions packages/generator/src/openapi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ export interface IConfig {
validation?: boolean;
conversion?: boolean;

endpointUrlPrefix?: string;
templates: Record<string, string>;
}

export interface IApiParserConfig {
endpointUrlPrefix?: string;
}
17 changes: 9 additions & 8 deletions packages/screens/src/navigation/lifecycleScreenNavigatorBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default abstract class LifecycleScreenNavigatorBase<
await this.callAll("onInitialize", context);
runInAction(() => (this.isInitializedValue = true));
} catch (error) {
console.error(error);
console.error("Error while calling onInitialize", error);
}
}

Expand All @@ -121,7 +121,7 @@ export default abstract class LifecycleScreenNavigatorBase<
await this.callAll("onActivate", context);
runInAction(() => (this.isActiveValue = true));
} catch (error) {
console.error(error);
console.error("Error while calling onActivate", error);
}
}

Expand Down Expand Up @@ -162,7 +162,7 @@ export default abstract class LifecycleScreenNavigatorBase<
await this.callAll("onDispose", context);
}
} catch (error) {
console.error(error);
console.error("Error while calling onDeactivate", error);
}
}

Expand All @@ -172,10 +172,10 @@ export default abstract class LifecycleScreenNavigatorBase<
event: T,
context: Parameters<HasLifecycleEvents[T]>[0]
): Promise<boolean> {
const screenFunction = this.screen?.[event];
const screenFunction = this.screen?.[event] as (context: NavigationContext<TScreen>) => Promise<boolean> | boolean;
if (typeof screenFunction === "function") {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const result = await screenFunction(context as any);
const result = await screenFunction.call(this.screen, context as any);
if (result === false) {
return false;
}
Expand All @@ -199,9 +199,10 @@ export default abstract class LifecycleScreenNavigatorBase<
event: T,
context: Parameters<HasLifecycleEvents[T]>[0]
): Promise<void> {
const screenFunction = this.screen?.[event];
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const screenFunctionPromise = typeof screenFunction === "function" ? screenFunction(context as any) : undefined;
const screenFunction = this.screen?.[event] as (context: NavigationContext<TScreen>) => Promise<unknown> | void;
const screenFunctionPromise =
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
typeof screenFunction === "function" ? screenFunction.call(this.screen, context as any) : undefined;

const listeners = this.eventHub?.getListeners(event);
if (listeners?.length) {
Expand Down

0 comments on commit 6ec9e7b

Please sign in to comment.