Compare commits

..

16 Commits

Author SHA1 Message Date
Ainar Garipov
1789c96c7f Pull request: all: doc v0.105.2
Merge in DNS/adguard-home from changelog to master

Squashed commit of the following:

commit 38ec49c2947ed666c1495f944c8056087d9f6dd5
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Mar 10 13:27:42 2021 +0300

    all: doc v0.105.2
2021-03-10 13:35:08 +03:00
Eugene Burkov
8811c8817e Pull request: upd dnsproxy
Merge in DNS/adguard-home from upd-dnsproxy to master

Squashed commit of the following:

commit 2181c08de3c2be1723770a5ffff091346536cafa
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Mar 9 18:55:32 2021 +0300

    all: upd dnsproxy
2021-03-09 19:09:29 +03:00
Eugene Burkov
5aa0ca9319 Pull request: 2582 invalid hostname vol.2
Merge in DNS/adguard-home from 2582-invalid-hostname-2 to master

Updates #2582.

Squashed commit of the following:

commit 9d3ceb289e3869b2c3d12e91ec104fb25d7931ee
Merge: 91c68e46 90054974
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Mar 5 19:11:49 2021 +0300

    Merge branch 'master' into 2582-invalid-hostname-2

commit 91c68e468c5f5b12a2fb509ff391133483c9d915
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Mar 5 18:28:14 2021 +0300

    all: mv trimming from home to dhcpd

commit f51faf35288577b6f610f172b26e7ac13aa24f72
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Mar 5 16:28:00 2021 +0300

    home: add more host sanitizings
2021-03-05 19:20:36 +03:00
Artem Baskal
90054974bc 2694: Statistics of request count of top clients not always displayed correctly
Close #2694

* commit '9f774d776c524fd7c3f0d270ad27ee8d234bf081':
  2694: Statistics of request count of top clients not always displayed correctly
2021-03-05 18:04:49 +03:00
Artem Baskal
9f774d776c 2694: Statistics of request count of top clients not always displayed correctly 2021-03-05 17:33:58 +03:00
Ainar Garipov
8ead755b67 Pull request: scripts: zero patch version number in snapshot versions
Merge in DNS/adguard-home from fix-version to master

Squashed commit of the following:

commit 3cf64a13c881987dd1f2cd679f7ef8558cefee9a
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Mar 4 19:48:17 2021 +0300

    scripts: zero patch version number in snapshot versions
2021-03-04 19:56:46 +03:00
Eugene Burkov
90ebc4d8c9 Pull request: 2582 invalid hostname
Merge in DNS/adguard-home from 2582-invalid-hostname to master

Updates #2582.

Squashed commit of the following:

commit 909598dae00588792b092f89c272c4487ba55dd1
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Mar 4 17:32:58 2021 +0300

    all: imp code quality, log changes

commit b3b970803709030c48cfe343b88d73524a043de3
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Mar 4 16:56:28 2021 +0300

    home: add host processing logic
2021-03-04 17:49:34 +03:00
Eugene Burkov
400b76d47b Pull request: 2681 fix DNS-over-TLS bug
Merge in DNS/adguard-home from 2681-fix-dot-bug to master

Updates #2681

Squashed commit of the following:

commit 8de0f4c9767218cd6956a2ab97eda6c5028d1d07
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Mar 3 19:25:34 2021 +0300

    all: upd dnsproxy
2021-03-03 19:55:37 +03:00
Ainar Garipov
8aa8be2921 Pull request: 2476 rwmutex
Updates #2476.

* commit '52575d0247d5411d26083d4c186d39d8098b916e':
  util: imp autohosts
  Use a RWMutex instead of a Mutex for authosts
2021-03-03 16:25:07 +03:00
Ainar Garipov
52575d0247 util: imp autohosts 2021-03-03 16:14:23 +03:00
Ainar Garipov
7e1b4ca6fe Merge branch 'master' into 2476-rwmutex 2021-03-03 16:10:29 +03:00
Eugene Burkov
a234b63da1 Pull request: 2600 inconsistent response
Merge in DNS/adguard-home from 2600-upd-dnsproxy to master

Closes #2600.

Squashed commit of the following:

commit 4b1515395841f14d86bc85b5c516d14919b5dc25
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Mar 2 18:39:19 2021 +0300

    all: upd dnsproxy again, log changes

commit 8a866201f140655b0d2f58552a7ad9bcae91a173
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Mar 2 14:53:48 2021 +0300

    all: cleanup

commit ae81234c79a6dbc61cccbae9c1b9d0144bb7f506
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Mar 2 14:49:28 2021 +0300

    all: fix deps

commit 662384c366feaf553d9eba2b5e3be93774631ec5
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Mar 2 13:58:26 2021 +0300

    all: upd dnsproxy version, fix functions' signatures
2021-03-03 15:27:25 +03:00
Eugene Burkov
94e783d572 Pull request: 2470 session token
Merge in DNS/adguard-home from 2470-session-token to master

Updates #2470.

Squashed commit of the following:

commit 02e874404808ee23000c49b4b2980b049dc4d0e6
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Mar 1 20:11:35 2021 +0300

    home: imp time formating

commit 6f4a6c9b190b2672cecd3e3e31413b03d19f8771
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Mar 1 18:48:15 2021 +0300

    home: rm user's data from session token
2021-03-01 20:37:28 +03:00
Eugene Burkov
91403d0b95 Pull request: 2757 fix OpenWRT detection
Merge in DNS/adguard-home from 2757-openwrt to master

Updates #2757.

Squashed commit of the following:

commit 8e94e6a67ae702bd1b281b306555a4ce9ecc6391
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Mar 1 17:02:24 2021 +0300

    util: convert only once

commit f1c74f4d18898f286d70c58f93b2fa21de6b5780
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Mar 1 16:22:51 2021 +0300

    util: log changes, imp docs

commit 0a4558d044602058255db71f825a730642cc9b07
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Mar 1 15:53:26 2021 +0300

    util: imp os detection
2021-03-01 17:20:16 +03:00
Ainar Garipov
d6a059e395 Pull request: openapi: add missing property
Merge in DNS/adguard-home from fix-openapi to master

Squashed commit of the following:

commit 24efa5732b327d179f07cbf67fbe8df6ee1b9477
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Feb 26 15:42:56 2021 +0300

    openapi: add missing property
2021-02-26 15:52:52 +03:00
jvoisin
f893df7e64 Use a RWMutex instead of a Mutex for authosts 2020-12-22 13:39:50 +01:00
22 changed files with 723 additions and 712 deletions

View File

@@ -13,19 +13,32 @@ and this project adheres to
## [v0.106.0] - 2021-04-26
-->
<!--
## [v0.105.2] - 2021-02-24
-->
## [v0.105.2] - 2021-03-10
### Fixed
- Incomplete hostnames with trailing zero-bytes handling ([#2582]).
- Wrong DNS-over-TLS ALPN configuration ([#2681]).
- Inconsistent responses for messages with EDNS0 and AD when DNS caching is
enabled ([#2600]).
- Incomplete OpenWRT detection ([#2757]).
- DHCP lease's `expired` field incorrect time format ([#2692]).
- Incomplete DNS upstreams validation ([#2674]).
- Wrong parsing of DHCP options of the `ip` type ([#2688]).
[#2582]: https://github.com/AdguardTeam/AdGuardHome/issues/2582
[#2600]: https://github.com/AdguardTeam/AdGuardHome/issues/2600
[#2674]: https://github.com/AdguardTeam/AdGuardHome/issues/2674
[#2681]: https://github.com/AdguardTeam/AdGuardHome/issues/2681
[#2688]: https://github.com/AdguardTeam/AdGuardHome/issues/2688
[#2692]: https://github.com/AdguardTeam/AdGuardHome/issues/2692
[#2757]: https://github.com/AdguardTeam/AdGuardHome/issues/2757
### Security
- Session token doesn't contain user's information anymore ([#2470]).
[#2470]: https://github.com/AdguardTeam/AdGuardHome/issues/2470
@@ -210,11 +223,12 @@ and this project adheres to
<!--
[Unreleased]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.105.2...HEAD
[v0.105.2]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.105.1...v0.105.2
[Unreleased]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.106.0...HEAD
[v0.106.0]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.105.2...v0.106.0
-->
[Unreleased]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.105.1...HEAD
[Unreleased]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.105.2...HEAD
[v0.105.2]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.105.1...v0.105.2
[v0.105.1]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.105.0...v0.105.1
[v0.105.0]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.104.3...v0.105.0
[v0.104.3]: https://github.com/AdguardTeam/AdGuardHome/compare/v0.104.2...v0.104.3

View File

@@ -3,7 +3,7 @@ import React, {
useEffect,
useRef,
} from 'react';
import { shallowEqual, useDispatch, useSelector } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import { useTranslation } from 'react-i18next';
import propTypes from 'prop-types';
import throttle from 'lodash/throttle';
@@ -25,19 +25,21 @@ const InfiniteTable = ({
const { t } = useTranslation();
const dispatch = useDispatch();
const loader = useRef(null);
const loadingRef = useRef(null);
const {
isEntireLog,
processingGetLogs,
} = useSelector((state) => state.queryLogs, shallowEqual);
const isEntireLog = useSelector((state) => state.queryLogs.isEntireLog);
const processingGetLogs = useSelector((state) => state.queryLogs.processingGetLogs);
const loading = isLoading || processingGetLogs;
const listener = useCallback(() => {
if (loader.current && isScrolledIntoView(loader.current)) {
if (!loadingRef.current && loader.current && isScrolledIntoView(loader.current)) {
dispatch(getLogs());
}
}, [loader.current, isScrolledIntoView, getLogs]);
}, []);
useEffect(() => {
loadingRef.current = processingGetLogs;
}, [processingGetLogs]);
useEffect(() => {
listener();

View File

@@ -76,7 +76,7 @@
"stylelint-webpack-plugin": "^2.1.1",
"terser-webpack-plugin": "^5.0.0",
"ts-loader": "^8.0.6",
"ts-morph": "^10.0.1",
"ts-morph": "^8.1.2",
"ts-node": "^9.0.0",
"typescript": "^4.0.3",
"url-loader": "^4.1.1",

View File

@@ -9,5 +9,4 @@ export const trimQuotes = (str: string) => {
return str.replace(/\'|\"/g, '');
};
export const GENERATOR_ENTITY_ALLIAS = 'Entities/';
export const BAD_REQUES_HELPER = 'BadRequesHelper';
export const GENERATOR_ENTITY_ALLIAS = 'Entities/';

View File

@@ -4,10 +4,9 @@ import { OPEN_API_PATH } from '../consts';
import EntitiesGenerator from './src/generateEntities';
import ApisGenerator from './src/generateApis';
import { OpenApi } from './src/utils';
const generateApi = (openApi: OpenApi) => {
const generateApi = (openApi: Record<string, any>) => {
const ent = new EntitiesGenerator(openApi);
ent.save();
@@ -15,5 +14,5 @@ const generateApi = (openApi: OpenApi) => {
api.save();
}
const openApiFile = fs.readFileSync('./scripts/generator/v1.yaml', 'utf8');
const openApiFile = fs.readFileSync(OPEN_API_PATH, 'utf8');
generateApi(YAML.parse(openApiFile));

View File

@@ -2,16 +2,15 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */
import * as fs from 'fs';
import * as path from 'path';
import { number } from 'prop-types';
import { stringify } from 'qs';
// eslint-disable-next-line import/no-extraneous-dependencies
import * as morph from 'ts-morph';
import {
API_DIR as API_DIR_CONST,
BAD_REQUES_HELPER,
GENERATOR_ENTITY_ALLIAS,
} from '../../consts';
import { toCamel, capitalize, schemaParamParser, OpenApi, uncapitalize, RequestBody } from './utils';
import { toCamel, capitalize, schemaParamParser } from './utils';
const API_DIR = path.resolve(API_DIR_CONST);
@@ -21,15 +20,11 @@ if (!fs.existsSync(API_DIR)) {
const { Project, QuoteKind } = morph;
enum PROCESS_AS {
JSON = 'JSON',
TEXT = 'TEXT',
EMPTY = 'EMPTY',
}
class ApiGenerator {
project = new Project({
tsConfigFilePath: './tsconfig.json',
addFilesFromTsConfig: false,
manipulationSettings: {
quoteKind: QuoteKind.Single,
usePrefixAndSuffixTextForRename: false,
@@ -37,7 +32,7 @@ class ApiGenerator {
},
});
openapi: OpenApi;
openapi: Record<string, any>;
serverUrl: string;
@@ -52,28 +47,20 @@ class ApiGenerator {
apis: morph.SourceFile[] = [];
methods = ['patch', 'delete', 'post', 'get', 'put', 'head', 'options', 'trace'];
constructor(openapi: OpenApi) {
constructor(openapi: Record<string, any>) {
this.openapi = openapi;
this.paths = openapi.paths;
this.serverUrl = openapi.servers[0].url;
Object.keys(this.paths).forEach((pathKey) => {
Object.keys(this.paths[pathKey]).filter((method) => this.methods.includes(method)).forEach((method) => {
Object.keys(this.paths[pathKey]).forEach((method) => {
const {
tags, operationId, responses, requestBody, security, "x-skip-web-api": skip
tags, operationId, parameters, responses, requestBody, security,
} = this.paths[pathKey][method];
const parameters = this.paths[pathKey][method].parameters || this.paths[pathKey].parameters;
const controller = toCamel((tags ? tags[0] : pathKey.split('/')[1]));
if (skip) {
return;
}
if (!operationId) {
console.log(pathKey);
}
const controller = toCamel((tags ? tags[0] : pathKey.split('/')[1]).replace('-controller', ''));
if (this.controllers[controller]) {
this.controllers[controller][uncapitalize(operationId)] = {
this.controllers[controller][operationId] = {
parameters,
responses,
method,
@@ -82,7 +69,7 @@ class ApiGenerator {
pathKey: pathKey.replace(/{/g, '${'),
};
} else {
this.controllers[controller] = { [uncapitalize(operationId)]: {
this.controllers[controller] = { [operationId]: {
parameters,
responses,
method,
@@ -110,7 +97,7 @@ class ApiGenerator {
]);
// const schemaProperties = schemas[schemaName].properties;
const importEntities: { type: string, isClass: boolean }[] = [];
const importEntities: any[] = [];
// add api class to file
const apiClass = apiFile.addClass({
@@ -124,34 +111,29 @@ class ApiGenerator {
// for each operation add fetcher
operationList.forEach((operation) => {
const {
requestBody, responses, parameters, method, pathKey,
requestBody, responses, parameters, method, pathKey, security,
} = controllerOperations[operation];
const queryParams: { name: string, type: string, hasQuestionToken: boolean }[] = [];
const bodyParam: { name: string, countedType: string, type?: string, isClass?: boolean, hasQuestionToken: boolean }[] = [];
let contentType: string = '';
const queryParams: any[] = []; // { name, type }
const bodyParam: any[] = []; // { name, type }
let hasResponseBodyType: /* boolean | ReturnType<schemaParamParser> */ false | [string, boolean, boolean, boolean, boolean] = false;
let contentType = '';
if (parameters) {
parameters.forEach((link: {$ref: string}) => {
const temp = link.$ref.split('/').pop()
const parameter = this.openapi.components.parameters[temp!];
const {
type, isArray, isClass, isImport,
} = schemaParamParser(parameter.schema, this.openapi);
parameters.forEach((p: any) => {
const [
pType, isArray, isClass, isImport,
] = schemaParamParser(p.schema, this.openapi);
if (isImport) {
importEntities.push({ type, isClass });
importEntities.push({ type: pType, isClass });
}
if (parameter.in === 'query') {
if (p.in === 'query') {
queryParams.push({
name: parameter.name, type: `${type}${isArray ? '[]' : ''}`, hasQuestionToken: !parameter.required });
name: p.name, type: `${pType}${isArray ? '[]' : ''}`, hasQuestionToken: !p.required });
}
});
}
if (queryParams.length > 0) {
const imp = apiFile.getImportDeclaration((i) => {
return i.getModuleSpecifierValue() === 'qs';
@@ -162,120 +144,62 @@ class ApiGenerator {
});
}
}
if (requestBody) {
let content = requestBody.content;
const { $ref }: { $ref: string } = requestBody;
const name = $ref.split('/').pop();
const { content, required } = this.openapi.components.requestBodies[name!];
if (!content && $ref) {
const name = $ref.split('/').pop() as string;
content = this.openapi.components.requestBodies[name].content;
}
[contentType] = Object.keys(content);
const data = content[contentType as keyof RequestBody['content']]!;
const data = content[contentType];
const {
type, isArray, isClass, isImport,
} = schemaParamParser(data.schema, this.openapi);
const [
pType, isArray, isClass, isImport,
] = schemaParamParser(data.schema, this.openapi);
if (isImport) {
importEntities.push({ type: type, isClass });
bodyParam.push({
name: type.toLowerCase(),
countedType: `${isClass ? 'I' : ''}${type}${isArray ? '[]' : ''}`,
isClass,
type,
hasQuestionToken: !required
});
importEntities.push({ type: pType, isClass });
bodyParam.push({ name: pType.toLowerCase(), type: `${isClass ? 'I' : ''}${pType}${isArray ? '[]' : ''}`, isClass, pType });
} else {
bodyParam.push({
name: 'data',
countedType: `${type}${isArray ? '[]' : ''}`,
hasQuestionToken: !required });
bodyParam.push({ name: 'data', type: `${pType}${isArray ? '[]' : ''}` });
}
}
if (responses['200']) {
const { content, headers } = responses['200'];
if (content && (content['*/*'] || content['application/json'])) {
const { schema, examples } = content['*/*'] || content['application/json'];
const responsesCodes = Object.keys(responses);
const responsesSchema = responsesCodes.map((code) => {
const refLink = responses[code].$ref.split('/').pop();
const ref = this.openapi.components.responses[refLink];
interface ResponseSchema {
code: number,
[PROCESS_AS.JSON]?: ReturnType<typeof schemaParamParser>;
[PROCESS_AS.TEXT]?: {
schema?: ReturnType<typeof schemaParamParser>;
xErrorCode?: string;
onlyText: boolean;
if (!schema) {
process.exit(0);
}
[PROCESS_AS.EMPTY]?: boolean;
}
const responseSchema: ResponseSchema = { code: Number(code) };
if (!ref.content) {
responseSchema[PROCESS_AS.EMPTY] = true;
return responseSchema;
}
if (ref.content?.['application/json']) {
const { schema } = ref.content['application/json'];
responseSchema[PROCESS_AS.JSON] = schemaParamParser(schema, this.openapi);
}
if (ref.content?.['text/palin']) {
const {
"x-error-class": xErrorClass,
"x-error-code": xErrorCode,
} = ref.content['text/palin'];
if (xErrorClass) {
const schemaLink = xErrorClass.split('/').pop();
const schema = this.openapi.components.schemas[schemaLink!];
responseSchema[PROCESS_AS.TEXT] = {
schema: schemaParamParser(schema, this.openapi),
xErrorCode,
onlyText: false,
}
} else {
responseSchema[PROCESS_AS.TEXT] = { onlyText: true };
}
}
return responseSchema;
});
const propType = schemaParamParser(schema, this.openapi);
const [pType, , isClass, isImport] = propType;
let returnTypes = new Set();
bodyParam.forEach((param) => {
if (param.isClass) {
returnTypes.add(BAD_REQUES_HELPER);
importEntities.push({ type: BAD_REQUES_HELPER, isClass: true });
}
})
responsesSchema.forEach((responseSchema) => {
if (responseSchema[PROCESS_AS.JSON]) {
const { type, isClass, isImport } = responseSchema[PROCESS_AS.JSON]!;
returnTypes.add(type);
if (isImport) {
importEntities.push({ type: type, isClass });
importEntities.push({ type: pType, isClass });
}
hasResponseBodyType = propType;
}
if (responseSchema[PROCESS_AS.TEXT]) {
const { onlyText, schema } = responseSchema[PROCESS_AS.TEXT]!;
if (onlyText) {
returnTypes.add('string');
} else {
const { type, isClass, isImport } = schema!;
returnTypes.add(type);
if (isImport) {
importEntities.push({ type, isClass });
}
}
}
if (responseSchema[PROCESS_AS.EMPTY]) {
returnTypes.add('number');
}
});
returnTypes.add('undefined');
const returnType = `Promise<${Array.from(returnTypes).join(' | ')}>`;
}
let returnType = '';
if (hasResponseBodyType) {
const [pType, isArray, isClass] = hasResponseBodyType as any;
let data = `Promise<${isClass ? 'I' : ''}${pType}${isArray ? '[]' : ''}`;
returnType = data;
} else {
returnType = 'Promise<number';
}
const shouldValidate = bodyParam.filter(b => b.isClass);
if (shouldValidate.length > 0) {
returnType += ' | string[]';
}
// append Error to default type return;
returnType += ' | Error>';
const fetcher = apiClass.addMethod({
isAsync: true,
@@ -287,19 +211,23 @@ class ApiGenerator {
fetcher.addParameters(params);
fetcher.setBodyText((w) => {
if (contentType === 'application/json') {
const shouldValidate = bodyParam.filter(b => b.isClass);
// Add data to URLSearchParams
if (contentType === 'text/plain') {
bodyParam.forEach((b) => {
w.writeLine(`const params = String(${b.name});`);
});
} else {
if (shouldValidate.length > 0) {
w.writeLine(`const haveError: string[] = [];`);
shouldValidate.forEach((b) => {
w.writeLine(`haveError.push(...${b.name}.validate());`);
w.writeLine(`const ${b.name}Valid = new ${b.pType}(${b.name});`);
w.writeLine(`haveError.push(...${b.name}Valid.validate());`);
});
w.writeLine(`if (haveError.length > 0) {`);
w.writeLine(` return Promise.resolve(new ${BAD_REQUES_HELPER}(haveError));`)
w.writeLine(` return Promise.resolve(haveError);`)
w.writeLine(`}`);
}
}
// Switch return of fetch in case on queryParams
if (queryParams.length > 0) {
w.writeLine('const queryParams = {');
@@ -315,36 +243,37 @@ class ApiGenerator {
w.writeLine(` method: '${method.toUpperCase()}',`);
// add Fetch options
if (contentType && contentType !== 'multipart/form-data') {
w.writeLine(' headers: {');
w.writeLine(` 'Content-Type': '${contentType}',`);
w.writeLine(' },');
}
if (contentType) {
w.writeLine(` body: JSON.stringify(${bodyParam.map((b) => b.isClass ? `${b.name}.serialize()` : b.name).join(', ')}),`);
switch (contentType) {
case 'text/plain':
w.writeLine(' body: params,');
break;
default:
w.writeLine(` body: JSON.stringify(${bodyParam.map((b) => b.isClass ? `${b.name}Valid.serialize()` : b.name).join(', ')}),`);
break;
}
}
w.writeLine('}).then(async (res) => {');
responsesSchema.forEach((responseSchema) => {
const { code } = responseSchema;
w.writeLine(` if (res.status === ${code}) {`);
if (responseSchema[PROCESS_AS.EMPTY]) {
w.writeLine(' return res.status;');
}
if (responseSchema[PROCESS_AS.TEXT]?.onlyText) {
w.writeLine(' return res.text();')
}
if (responseSchema[PROCESS_AS.JSON] && responseSchema[PROCESS_AS.TEXT]) {
const { type } = responseSchema[PROCESS_AS.JSON]!;
const { schema, xErrorCode } = responseSchema[PROCESS_AS.TEXT]!;
const { type: errType } = schema!;
w.writeLine(' try {');
w.writeLine(` return new ${type}(await res.json());`);
w.writeLine(' } catch {');
w.writeLine(` return new ${errType}({ msg: await res.text() code: ${xErrorCode}} as any);`);
w.writeLine(' }');
}
if (responseSchema[PROCESS_AS.JSON]) {
const { type } = responseSchema[PROCESS_AS.JSON]!;
w.writeLine(` return new ${type}(await res.json());`);
}
w.writeLine(` }`);
})
// Handle response
if (hasResponseBodyType) {
w.writeLine('}).then(async (res) => {');
w.writeLine(' if (res.status === 200) {');
w.writeLine(' return res.json();');
} else {
w.writeLine('}).then(async (res) => {');
w.writeLine(' if (res.status === 200) {');
w.writeLine(' return res.status;');
}
// Handle Error
w.writeLine(' } else {');
w.writeLine(' return new Error(String(res.status));');
w.writeLine(' }');
w.writeLine('})');
});
});
@@ -359,16 +288,17 @@ class ApiGenerator {
}
});
imports.sort((a,b) => a.type > b.type ? 1 : -1).forEach((ie) => {
const { type: type, isClass } = ie;
const { type: pType, isClass } = ie;
if (isClass) {
apiFile.addImportDeclaration({
moduleSpecifier: `${GENERATOR_ENTITY_ALLIAS}${type}`,
defaultImport: type,
moduleSpecifier: `${GENERATOR_ENTITY_ALLIAS}${pType}`,
defaultImport: pType,
namedImports: [`I${pType}`],
});
} else {
apiFile.addImportDeclaration({
moduleSpecifier: `${GENERATOR_ENTITY_ALLIAS}${type}`,
namedImports: [type],
moduleSpecifier: `${GENERATOR_ENTITY_ALLIAS}${pType}`,
namedImports: [pType],
});
}
});

View File

@@ -3,8 +3,8 @@ import * as path from 'path';
// eslint-disable-next-line import/no-extraneous-dependencies
import * as morph from 'ts-morph';
import { ENT_DIR, BAD_REQUES_HELPER } from '../../consts';
import { TYPES, toCamel, schemaParamParser, capitalize, OpenApi, Schema } from './utils';
import { ENT_DIR } from '../../consts';
import { TYPES, toCamel, schemaParamParser, uncapitalize } from './utils';
const { Project, QuoteKind } = morph;
@@ -17,6 +17,7 @@ if (!fs.existsSync(EntDir)) {
class EntitiesGenerator {
project = new Project({
tsConfigFilePath: './tsconfig.json',
addFilesFromTsConfig: false,
manipulationSettings: {
quoteKind: QuoteKind.Single,
usePrefixAndSuffixTextForRename: false,
@@ -24,480 +25,491 @@ class EntitiesGenerator {
},
});
openapi: OpenApi;
openapi: Record<string, any>;
schemas: Record<string, Schema>;
schemas: Record<string, any>;
schemaNames: string[];
entities: morph.SourceFile[] = [];
constructor(openapi: OpenApi) {
constructor(openapi: Record<string, any>) {
this.openapi = openapi;
this.schemas = openapi.components.schemas;
this.schemaNames = Object.keys(this.schemas);
this.generateEntities();
this.generateUtils();
}
generateUtils = () => {
const helperFile = this.project.createSourceFile(`${EntDir}/${BAD_REQUES_HELPER}.ts`);
helperFile.addImportDeclaration({
moduleSpecifier: `./BadRequestResp`,
defaultImport: 'BadRequestResp',
});
helperFile.addImportDeclaration({
moduleSpecifier: `./ErrorCode`,
namedImports: ['ErrorCode'],
});
const helperClass = helperFile.addClass({
name: 'BadRequestHelper',
isDefaultExport: true,
extends: 'BadRequestResp',
properties: [{
type: 'string[]',
name: 'fields'
}]
});
const helperConstructor = helperClass.addConstructor({
parameters: [{
type: 'string[]',
name: 'fields'
}],
});
helperConstructor.setBodyText((w) => {
w.writeLine('super({ code: ErrorCode.JSN001, msg: \'Wrong fields value\' });');
w.writeLine('this.fields = fields;')
});
this.entities.push(helperFile);
}
generateEntities = () => {
this.schemaNames.forEach(this.generateEntity);
};
generateEntity = (schemaName: string) => {
const { properties, type, oneOf, enum: en } = this.schemas[schemaName];
generateEntity = (sName: string) => {
const { properties, type, oneOf } = this.schemas[sName];
const notAClass = !properties && TYPES[type as keyof typeof TYPES];
if (oneOf) {
this.generateOneOf(schemaName);
this.generateOneOf(sName);
return;
}
if (en) {
this.generateEnum(schemaName);
return;
}
if (notAClass) {
this.generatePrimitive(schemaName)
this.generateEnum(sName);
} else {
this.generateClass(schemaName);
this.generateClass(sName);
}
};
generatePrimitive = (schemaName: string) => {
const entityFile = this.project.createSourceFile(`${EntDir}/${schemaName}.ts`);
generateEnum = (sName: string) => {
const entityFile = this.project.createSourceFile(`${EntDir}/${sName}.ts`);
entityFile.addStatements([
'// This file was autogenerated. Please do not change.',
'// All changes will be overwrited on commit.',
'',
]);
const { type: schemaType, description, pattern } = this.schemas[schemaName];
if (description) {
entityFile.addStatements(['\n/*', `Description: ${description}`, '*/\n']);
}
if (pattern) {
entityFile.addStatements(`const pattern = new RegExp('${pattern}')`);
}
const type: string = TYPES[schemaType as keyof typeof TYPES];
const entityClass = entityFile.addClass({
name: schemaName,
isDefaultExport: true,
extends: capitalize(type),
});
const ctor = entityClass.addConstructor({
parameters: [{
name: 'v',
type,
}],
});
ctor.setBodyText((w) => {
const { minLength, minimum, maxLength, maximum } = this.schemas[schemaName];
if (type === 'string') {
if (pattern) {
w.writeLine('if (!v.match(pattern)) {');
w.writeLine(' throw new Error();');
w.writeLine('}');
}
if (typeof minLength === 'number') {
w.writeLine(`if (v.length < ${minLength}) {`);
w.writeLine(' throw new Error();');
w.writeLine('}');
}
if (typeof maxLength === 'number') {
w.writeLine(`if (v.length > ${maxLength}) {`);
w.writeLine(' throw new Error();');
w.writeLine('}');
}
}
if (type === 'number') {
if (typeof minimum === 'number') {
w.writeLine(`if (v.length < ${minimum}) {`);
w.writeLine(' throw new Error();');
w.writeLine('}');
}
if (typeof maximum === 'number') {
w.writeLine(`if (v.length > ${maximum}) {`);
w.writeLine(' throw new Error();');
w.writeLine('}');
}
}
w.writeLine('super(v);');
});
this.entities.push(entityFile);
};
generateEnum = (schemaName: string) => {
const entityFile = this.project.createSourceFile(`${EntDir}/${schemaName}.ts`);
entityFile.addStatements([
'// This file was autogenerated. Please do not change.',
'',
]);
const { enum: enumMembers, description, example } = this.schemas[schemaName];
if (description) {
entityFile.addStatements(['\n/*', `Description: ${description}`, '*/\n']);
}
const { enum: enumMembers } = this.schemas[sName];
entityFile.addEnum({
name: schemaName,
members: enumMembers!.map((e: string) => ({ name: e.toUpperCase(), value: e })),
name: sName,
members: enumMembers.map((e: string) => ({ name: e.toUpperCase(), value: e })),
isExported: true,
});
this.entities.push(entityFile);
};
generateOneOf = (schemaName: string) => {
const entityFile = this.project.createSourceFile(`${EntDir}/${schemaName}.ts`);
generateOneOf = (sName: string) => {
const entityFile = this.project.createSourceFile(`${EntDir}/${sName}.ts`);
entityFile.addStatements([
'// This file was autogenerated. Please do not change.',
'// All changes will be overwrited on commit.',
'',
]);
const importEntities: { type: string, isClass: boolean }[] = [];
const entities = this.schemas[schemaName].oneOf.map((elem: any) => {
const {
type: type, isArray, isClass, isImport,
} = schemaParamParser(elem, this.openapi);
importEntities.push({ type: type, isClass });
return { type: type, isArray };
const entities = this.schemas[sName].oneOf.map((elem: any) => {
const [
pType, isArray, isClass, isImport,
] = schemaParamParser(elem, this.openapi);
importEntities.push({ type: pType, isClass });
return { type: pType, isArray };
});
entityFile.addTypeAlias({
name: schemaName,
name: sName,
isExported: true,
type: entities.map((e: any) => e.isArray ? `I${e.type}[]` : `I${e.type}`).join(' | '),
})
// add import
importEntities.sort((a, b) => a.type > b.type ? 1 : -1).forEach((ie) => {
const { type: type, isClass } = ie;
const { type: pType, isClass } = ie;
if (isClass) {
entityFile.addImportDeclaration({
moduleSpecifier: `./${type}`,
namedImports: [`I${type}`],
moduleSpecifier: `./${pType}`,
namedImports: [`I${pType}`],
});
} else {
entityFile.addImportDeclaration({
moduleSpecifier: `./${type}`,
namedImports: [type],
moduleSpecifier: `./${pType}`,
namedImports: [pType],
});
}
});
this.entities.push(entityFile);
}
generateClass = (schemaName: string) => {
const entityFile = this.project.createSourceFile(`${EntDir}/${schemaName}.ts`);
generateClass = (sName: string) => {
const entityFile = this.project.createSourceFile(`${EntDir}/${sName}.ts`);
entityFile.addStatements([
'// This file was autogenerated. Please do not change.',
'// All changes will be overwrited on commit.',
'',
]);
let { properties, required, allOf, $ref } = this.schemas[schemaName];
if (allOf) {
const refLink: string = allOf.find((obj: Record<string, any>) => obj.$ref).$ref;
let ref: any = refLink.split('/')
ref = ref.pop();
const reasign = allOf.find((obj: Record<string, any>) => !obj.$ref);
const newSchema: Schema = { ...this.schemas[ref], ...reasign };
properties = newSchema.properties;
required = newSchema.required;
}
const { properties: sProps, required, $ref, additionalProperties } = this.schemas[sName];
if ($ref) {
const refLink = $ref.split('/').pop()!;
const temp = $ref.split('/');
const importSchemaName = `${temp[temp.length - 1]}`;
entityFile.addImportDeclaration({
defaultImport: refLink,
moduleSpecifier: `./${refLink}`,
namedImports: [`I${refLink}`],
defaultImport: importSchemaName,
moduleSpecifier: `./${importSchemaName}`,
namedImports: [`I${importSchemaName}`],
});
entityFile.addTypeAlias({
name: `I${schemaName}`,
type: `I${refLink}`,
name: `I${sName}`,
type: `I${importSchemaName}`,
isExported: true,
})
const entityClass = entityFile.addClass({
name: schemaName,
isDefaultExport: true,
extends: refLink,
})
const ctor = entityClass.addConstructor({
parameters: [{
name: 'props',
type: `I${schemaName}`,
}],
})
ctor.setBodyText((w) => {
w.writeLine('super(props);')
});
entityFile.addStatements(`export default ${importSchemaName};`);
this.entities.push(entityFile);
return;
}
const importEntities: { type: string, isClass: boolean }[] = [];
const entityInterface = entityFile.addInterface({
name: `I${schemaName}`,
name: `I${sName}`,
isExported: true,
});
const sortedProperties = Object.keys(properties || {}).sort();
let importEntities: { type: string, isClass: boolean }[] = [];
type SortedPropertiesTypesValues = ReturnType<typeof schemaParamParser> & {
computedType: string;
isRequired: boolean;
}
const sortedPropertiesTypes = sortedProperties.reduce((data, propertyName) => {
const isRequired = !!(required && required.includes(propertyName));
const parsed = schemaParamParser(properties![propertyName], this.openapi);
data[propertyName] = {
...parsed,
isRequired,
computedType: `${parsed.type}${parsed.isArray ? '[]' : ''}${isRequired ? '' : ' | undefined'}`
};
return data;
}, {} as Record<string, SortedPropertiesTypesValues>);
const sortedSProps = Object.keys(sProps || {}).sort();
const additionalPropsOnly = additionalProperties && sortedSProps.length === 0;
// add server response interface to entityFile
sortedProperties.forEach((propertyName) => {
const {
type, isArray, isClass, isImport
} = sortedPropertiesTypes[propertyName];
sortedSProps.forEach((sPropName) => {
const [
pType, isArray, isClass, isImport, isAdditional
] = schemaParamParser(sProps[sPropName], this.openapi);
if (isImport) {
importEntities.push({ type: type, isClass });
importEntities.push({ type: pType, isClass });
}
const propertyType = isAdditional
? `{ [key: string]: ${isClass ? 'I' : ''}${pType}${isArray ? '[]' : ''} }`
: `${isClass ? 'I' : ''}${pType}${isArray ? '[]' : ''}`;
entityInterface.addProperty({
name: propertyName,
type: `${isClass ? 'I' : ''}${type}${isArray ? '[]' : ''}`,
name: sPropName,
type: propertyType,
hasQuestionToken: !(
(required && required.includes(propertyName)) || properties![propertyName].required
(required && required.includes(sPropName)) || sProps[sPropName].required
),
});
});
if (additionalProperties) {
const [
pType, isArray, isClass, isImport, isAdditional
] = schemaParamParser(additionalProperties, this.openapi);
if (isImport) {
importEntities.push({ type: pType, isClass });
}
const type = isAdditional
? `{ [key: string]: ${isClass ? 'I' : ''}${pType}${isArray ? '[]' : ''} }`
: `${isClass ? 'I' : ''}${pType}${isArray ? '[]' : ''}`;
entityInterface.addIndexSignature({
keyName: 'key',
keyType: 'string',
returnType: additionalPropsOnly ? type : `${type} | undefined`,
});
}
// add import
const imports: { type: string, isClass: boolean }[] = [];
const types: string[] = [];
importEntities = importEntities.filter((i) => {
importEntities.forEach((i) => {
const { type } = i;
if (!types.includes(type)) {
imports.push(i);
types.push(type);
return true;
}
return false;
});
importEntities.sort((a, b) => a.type > b.type ? 1 : -1).forEach((ie) => {
const { type: type, isClass } = ie;
imports.sort((a, b) => a.type > b.type ? 1 : -1).forEach((ie) => {
const { type: pType, isClass } = ie;
if (isClass) {
entityFile.addImportDeclaration({
defaultImport: type,
moduleSpecifier: `./${type}`,
namedImports: [`I${type}`],
defaultImport: pType,
moduleSpecifier: `./${pType}`,
namedImports: [`I${pType}`],
});
} else {
entityFile.addImportDeclaration({
moduleSpecifier: `./${type}`,
namedImports: [type],
moduleSpecifier: `./${pType}`,
namedImports: [pType],
});
}
});
const entityClass = entityFile.addClass({
name: schemaName,
name: sName,
isDefaultExport: true,
});
// addProperties to class;
sortedProperties.forEach((propertyName) => {
const { type, isArray, isClass, isEnum, isRequired, computedType } = sortedPropertiesTypes[propertyName];
sortedSProps.forEach((sPropName) => {
const [pType, isArray, isClass, isImport, isAdditional] = schemaParamParser(sProps[sPropName], this.openapi);
const isRequred = (required && required.includes(sPropName))
|| sProps[sPropName].required;
const propertyType = isAdditional
? `{ [key: string]: ${pType}${isArray ? '[]' : ''}${isRequred ? '' : ' | undefined'} }`
: `${pType}${isArray ? '[]' : ''}${isRequred ? '' : ' | undefined'}`;
entityClass.addProperty({
name: `_${propertyName}`,
name: `_${sPropName}`,
isReadonly: true,
type: computedType,
type: propertyType,
});
const getter = entityClass.addGetAccessor({
name: toCamel(propertyName),
returnType: computedType,
statements: [`return this._${propertyName};`],
name: toCamel(sPropName),
returnType: propertyType,
statements: [`return this._${sPropName};`],
});
const { description, example, minItems, maxItems, maxLength, minLength, maximum, minimum } = properties![propertyName];
const { description, example, minItems, maxItems, maxLength, minLength, maximum, minimum } = sProps[sPropName];
if (description || example) {
getter.addJsDoc(`${example ? `Description: ${description}` : ''}${example ? `\nExample: ${example}` : ''}`);
}
if (minItems) {
entityClass.addProperty({
entityClass.addGetAccessor({
isStatic: true,
isReadonly: true,
name: `${capitalize(toCamel(propertyName))}MinItems`,
initializer: `${minItems}`,
name: `${toCamel(sPropName)}MinItems`,
statements: [`return ${minItems};`],
});
}
if (maxItems) {
entityClass.addProperty({
entityClass.addGetAccessor({
isStatic: true,
isReadonly: true,
name: `${capitalize(toCamel(propertyName))}MaxItems`,
initializer: `${maxItems}`,
name: `${toCamel(sPropName)}MaxItems`,
statements: [`return ${maxItems};`],
});
}
if (typeof minLength === 'number') {
entityClass.addProperty({
entityClass.addGetAccessor({
isStatic: true,
isReadonly: true,
name: `${capitalize(toCamel(propertyName))}MinLength`,
initializer: `${minLength}`,
name: `${toCamel(sPropName)}MinLength`,
statements: [`return ${minLength};`],
});
}
if (maxLength) {
entityClass.addProperty({
entityClass.addGetAccessor({
isStatic: true,
isReadonly: true,
name: `${capitalize(toCamel(propertyName))}MaxLength`,
initializer: `${maxLength}`,
name: `${toCamel(sPropName)}MaxLength`,
statements: [`return ${maxLength};`],
});
}
if (typeof minimum === 'number') {
entityClass.addProperty({
entityClass.addGetAccessor({
isStatic: true,
isReadonly: true,
name: `${capitalize(toCamel(propertyName))}MinValue`,
initializer: `${minimum}`,
name: `${toCamel(sPropName)}MinValue`,
statements: [`return ${minimum};`],
});
}
if (maximum) {
entityClass.addProperty({
entityClass.addGetAccessor({
isStatic: true,
isReadonly: true,
name: `${capitalize(toCamel(propertyName))}MaxValue`,
initializer: `${maximum}`,
name: `${toCamel(sPropName)}MaxValue`,
statements: [`return ${maximum};`],
});
}
});
if (!(isArray && isClass) && !isClass) {
const isEnum = !isClass && isImport;
const isRequired = (required && required.includes(sPropName)) || sProps[sPropName].required;
const { maxLength, minLength, maximum, minimum } = sProps[sPropName];
const haveValidationFields = maxLength || typeof minLength === 'number' || maximum || typeof minimum === 'number';
if (isRequired || haveValidationFields) {
const prop = toCamel(sPropName);
const validateField = entityClass.addMethod({
isStatic: true,
name: `${prop}Validate`,
returnType: `boolean`,
parameters: [{
name: prop,
type: `${pType}${isArray ? '[]' : ''}${isRequred ? '' : ' | undefined'}`,
}],
})
validateField.setBodyText((w) => {
w.write('return ');
const nonRequiredCall = isRequired ? prop : `!${prop} ? true : ${prop}`;
if (pType === 'string') {
if (isArray) {
w.write(`${nonRequiredCall}.reduce<boolean>((result, p) => result && (typeof p === 'string' && !!p.trim()), true)`);
} else {
if (typeof minLength === 'number' && maxLength) {
w.write(`(${nonRequiredCall}.length >${minLength > 0 ? '=' : ''} ${minLength}) && (${nonRequiredCall}.length <= ${maxLength})`);
}
if (typeof minLength !== 'number' || !maxLength) {
w.write(`${isRequired ? `typeof ${prop} === 'string'` : `!${prop} ? true : typeof ${prop} === 'string'`} && !!${nonRequiredCall}.trim()`);
}
}
} else if (pType === 'number') {
if (isArray) {
w.write(`${nonRequiredCall}.reduce<boolean>((result, p) => result && typeof p === 'number', true)`);
} else {
if (typeof minimum === 'number' && maximum) {
w.write(`${isRequired ? `${prop} >= ${minimum} && ${prop} <= ${maximum}` : `!${prop} ? true : ((${prop} >= ${minimum}) && (${prop} <= ${maximum}))`}`);
}
if (typeof minimum !== 'number' || !maximum) {
w.write(`${isRequired ? `typeof ${prop} === 'number'` : `!${prop} ? true : typeof ${prop} === 'number'`}`);
}
}
} else if (pType === 'boolean') {
w.write(`${isRequired ? `typeof ${prop} === 'boolean'` : `!${prop} ? true : typeof ${prop} === 'boolean'`}`);
} else if (isEnum) {
if (isArray){
w.write(`${nonRequiredCall}.reduce<boolean>((result, p) => result && Object.keys(${pType}).includes(${prop}), true)`);
} else {
w.write(`${isRequired ? `Object.keys(${pType}).includes(${prop})` : `!${prop} ? true : typeof ${prop} === 'boolean'`}`);
}
}
w.write(';');
});
}
}
});
if (additionalProperties) {
const [
pType, isArray, isClass, isImport, isAdditional
] = schemaParamParser(additionalProperties, this.openapi);
const type = `Record<string, ${pType}${isArray ? '[]' : ''}>`;
entityClass.addProperty({
name: additionalPropsOnly ? 'data' : `${uncapitalize(pType)}Data`,
isReadonly: true,
type: type,
});
}
// add constructor;
const ctor = entityClass.addConstructor({
parameters: [{
name: 'props',
type: `I${schemaName}`,
type: `I${sName}`,
}],
});
ctor.setBodyText((w) => {
sortedProperties.forEach((propertyName) => {
const { type, isArray, isClass, isRequired } = sortedPropertiesTypes[propertyName];
const indent = !isRequired ? ' ' : '';
if (!isRequired) {
if ((type === 'boolean' || type === 'number' || type ==='string') && !isClass && !isArray) {
w.writeLine(`if (typeof props.${propertyName} === '${type}') {`);
} else {
w.writeLine(`if (props.${propertyName}) {`);
}
}
if (isArray && isClass) {
w.writeLine(`${indent}this._${propertyName} = props.${propertyName}.map((p) => new ${type}(p));`);
} else if (isClass) {
w.writeLine(`${indent}this._${propertyName} = new ${type}(props.${propertyName});`);
if (additionalProperties) {
const [
pType, isArray, isClass, isImport, isAdditional
] = schemaParamParser(additionalProperties, this.openapi);
w.writeLine(`this.${additionalPropsOnly ? 'data' : `${uncapitalize(pType)}Data`} = Object.entries(props).reduce<Record<string, ${pType}>>((prev, [key, value]) => {`);
if (isClass) {
w.writeLine(` prev[key] = new ${pType}(value!);`);
} else {
if (type === 'string' && !isArray) {
w.writeLine(`${indent}this._${propertyName} = props.${propertyName}.trim();`);
w.writeLine(' prev[key] = value!;')
}
w.writeLine(' return prev;');
w.writeLine('}, {})');
return;
}
sortedSProps.forEach((sPropName) => {
const [
pType, isArray, isClass, , isAdditional
] = schemaParamParser(sProps[sPropName], this.openapi);
const req = (required && required.includes(sPropName))
|| sProps[sPropName].required;
if (!req) {
if ((pType === 'boolean' || pType === 'number' || pType ==='string') && !isClass && !isArray) {
w.writeLine(`if (typeof props.${sPropName} === '${pType}') {`);
} else {
w.writeLine(`${indent}this._${propertyName} = props.${propertyName};`);
w.writeLine(`if (props.${sPropName}) {`);
}
}
if (!isRequired) {
if (isAdditional) {
if (isArray && isClass) {
w.writeLine(`${!req ? ' ' : ''}this._${sPropName} = props.${sPropName}.map((p) => Object.keys(p).reduce((prev, key) => {
return { ...prev, [key]: new ${pType}(p[key])};
},{}))`);
} else if (isClass) {
w.writeLine(`${!req ? ' ' : ''}this._${sPropName} = Object.keys(props.${sPropName}).reduce((prev, key) => {
return { ...prev, [key]: new ${pType}(props.${sPropName}[key])};
},{})`);
} else {
if (pType === 'string' && !isArray) {
w.writeLine(`${!req ? ' ' : ''}this._${sPropName} = Object.keys(props.${sPropName}).reduce((prev, key) => {
return { ...prev, [key]: props.${sPropName}[key].trim()};
},{})`);
} else {
w.writeLine(`${!req ? ' ' : ''}this._${sPropName} = Object.keys(props.${sPropName}).reduce((prev, key) => {
return { ...prev, [key]: props.${sPropName}[key]};
},{})`);
}
}
} else {
if (isArray && isClass) {
w.writeLine(`${!req ? ' ' : ''}this._${sPropName} = props.${sPropName}.map((p) => new ${pType}(p));`);
} else if (isClass) {
w.writeLine(`${!req ? ' ' : ''}this._${sPropName} = new ${pType}(props.${sPropName});`);
} else {
if (pType === 'string' && !isArray) {
w.writeLine(`${!req ? ' ' : ''}this._${sPropName} = props.${sPropName}.trim();`);
} else {
w.writeLine(`${!req ? ' ' : ''}this._${sPropName} = props.${sPropName};`);
}
}
}
if (!req) {
w.writeLine('}');
}
});
});
// add serialize method;
const serialize = entityClass.addMethod({
isStatic: false,
name: 'serialize',
returnType: `I${schemaName}`,
returnType: `I${sName}`,
});
serialize.setBodyText((w) => {
w.writeLine(`const data: I${schemaName} = {`);
if (additionalProperties) {
const [
pType, isArray, isClass, isImport, isAdditional
] = schemaParamParser(additionalProperties, this.openapi);
w.writeLine(`return Object.entries(this.${additionalPropsOnly ? 'data' : `${uncapitalize(pType)}Data`}).reduce<Record<string, ${isClass ? 'I' : ''}${pType}>>((prev, [key, value]) => {`);
if (isClass) {
w.writeLine(` prev[key] = value.serialize();`);
} else {
w.writeLine(' prev[key] = value;')
}
w.writeLine(' return prev;');
w.writeLine('}, {})');
return;
}
w.writeLine(`const data: I${sName} = {`);
const unReqFields: string[] = [];
sortedProperties.forEach((propertyName) => {
const {isArray, isClass, isRequired } = sortedPropertiesTypes[propertyName];
if (!isRequired) {
unReqFields.push(propertyName);
sortedSProps.forEach((sPropName) => {
const req = (required && required.includes(sPropName))
|| sProps[sPropName].required;
const [, isArray, isClass, , isAdditional] = schemaParamParser(sProps[sPropName], this.openapi);
if (!req) {
unReqFields.push(sPropName);
return;
}
if (isArray && isClass) {
w.writeLine(` ${propertyName}: this._${propertyName}.map((p) => p.serialize()),`);
} else if (isClass) {
w.writeLine(` ${propertyName}: this._${propertyName}.serialize(),`);
if (isAdditional) {
if (isArray && isClass) {
w.writeLine(` ${sPropName}: this._${sPropName}.map((p) => Object.keys(p).reduce((prev, key) => ({ ...prev, [key]: p[key].serialize() }))),`);
} else if (isClass) {
w.writeLine(` ${sPropName}: Object.keys(this._${sPropName}).reduce<Record<string, any>>((prev, key) => ({ ...prev, [key]: this._${sPropName}[key].serialize() }), {}),`);
} else {
w.writeLine(` ${sPropName}: Object.keys(this._${sPropName}).reduce((prev, key) => ({ ...prev, [key]: this._${sPropName}[key] })),`);
}
} else {
w.writeLine(` ${propertyName}: this._${propertyName},`);
if (isArray && isClass) {
w.writeLine(` ${sPropName}: this._${sPropName}.map((p) => p.serialize()),`);
} else if (isClass) {
w.writeLine(` ${sPropName}: this._${sPropName}.serialize(),`);
} else {
w.writeLine(` ${sPropName}: this._${sPropName},`);
}
}
});
w.writeLine('};');
unReqFields.forEach((propertyName) => {
const { isArray, isClass } = sortedPropertiesTypes[propertyName];
w.writeLine(`if (typeof this._${propertyName} !== 'undefined') {`);
if (isArray && isClass) {
w.writeLine(` data.${propertyName} = this._${propertyName}.map((p) => p.serialize());`);
} else if (isClass) {
w.writeLine(` data.${propertyName} = this._${propertyName}.serialize();`);
unReqFields.forEach((sPropName) => {
const [, isArray, isClass, , isAdditional] = schemaParamParser(sProps[sPropName], this.openapi);
w.writeLine(`if (typeof this._${sPropName} !== 'undefined') {`);
if (isAdditional) {
if (isArray && isClass) {
w.writeLine(` data.${sPropName} = this._${sPropName}.map((p) => Object.keys(p).reduce((prev, key) => ({ ...prev, [key]: p[key].serialize() }), {}));`);
} else if (isClass) {
w.writeLine(` data.${sPropName} = Object.keys(this._${sPropName}).reduce((prev, key) => ({ ...prev, [key]: this._${sPropName}[key].serialize() }), {});`);
} else {
w.writeLine(` data.${sPropName} = Object.keys(this._${sPropName}).reduce((prev, key) => ({ ...prev, [key]: this._${sPropName}[key] }), {});`);
}
} else {
w.writeLine(` data.${propertyName} = this._${propertyName};`);
if (isArray && isClass) {
w.writeLine(` data.${sPropName} = this._${sPropName}.map((p) => p.serialize());`);
} else if (isClass) {
w.writeLine(` data.${sPropName} = this._${sPropName}.serialize();`);
} else {
w.writeLine(` data.${sPropName} = this._${sPropName};`);
}
}
w.writeLine(`}`);
});
w.writeLine('return data;');
@@ -510,55 +522,74 @@ class EntitiesGenerator {
returnType: `string[]`,
})
validate.setBodyText((w) => {
w.writeLine('const validateRequired = {');
Object.keys(properties || {}).forEach((propertyName) => {
const { isArray, isClass, type, isRequired } = sortedPropertiesTypes[propertyName];
const { maxLength, minLength, maximum, minimum } = properties![propertyName];
if (additionalPropsOnly) {
w.writeLine('return []')
return;
}
w.writeLine('const validate = {');
Object.keys(sProps || {}).forEach((sPropName) => {
const [pType, isArray, isClass, , isAdditional] = schemaParamParser(sProps[sPropName], this.openapi);
const nonRequiredCall = isRequired ? `this._${propertyName}` : `!this._${propertyName} ? true : this._${propertyName}`;
const { maxLength, minLength, maximum, minimum } = sProps[sPropName];
const isRequired = (required && required.includes(sPropName)) || sProps[sPropName].required;
const nonRequiredCall = isRequired ? `this._${sPropName}` : `!this._${sPropName} ? true : this._${sPropName}`;
if (isArray && isClass) {
w.writeLine(` ${propertyName}: ${nonRequiredCall}.reduce<boolean>((result, p) => result && p.validate().length === 0, true),`);
} else if (isClass) {
w.writeLine(` ${propertyName}: ${nonRequiredCall}.validate().length === 0,`);
w.writeLine(` ${sPropName}: ${nonRequiredCall}.reduce((result, p) => result && p.validate().length === 0, true),`);
} else if (isClass && !isAdditional) {
w.writeLine(` ${sPropName}: ${nonRequiredCall}.validate().length === 0,`);
} else {
if (type === 'string') {
if (pType === 'string') {
if (isArray) {
w.writeLine(` ${propertyName}: ${nonRequiredCall}.reduce<boolean>((result, p) => result && typeof p === 'string', true),`);
w.writeLine(` ${sPropName}: ${nonRequiredCall}.reduce((result, p) => result && typeof p === 'string', true),`);
} else {
if (typeof minLength === 'number' && maxLength) {
w.writeLine(` ${propertyName}: (${nonRequiredCall}.length >${minLength > 0 ? '=' : ''} ${minLength}) && (${nonRequiredCall}.length <= ${maxLength}),`);
w.writeLine(` ${sPropName}: (${nonRequiredCall}.length >${minLength > 0 ? '=' : ''} ${minLength}) && (${nonRequiredCall}.length <= ${maxLength}),`);
}
if (typeof minLength !== 'number' || !maxLength) {
w.writeLine(` ${propertyName}: ${isRequired ? `typeof this._${propertyName} === 'string' && !!this._${propertyName}.trim()` : `!this._${propertyName} ? true : typeof this._${propertyName} === 'string'`},`);
w.writeLine(` ${sPropName}: ${isRequired ? `typeof this._${sPropName} === 'string'` : `!this._${sPropName} ? true : typeof this._${sPropName} === 'string'`} && !this._${sPropName} ? true : this._${sPropName},`);
}
}
} else if (type === 'number') {
} else if (pType === 'number') {
if (isArray) {
w.writeLine(` ${propertyName}: ${nonRequiredCall}.reduce<boolean>((result, p) => result && typeof p === 'number', true),`);
w.writeLine(` ${sPropName}: ${nonRequiredCall}.reduce((result, p) => result && typeof p === 'number', true),`);
} else {
if (typeof minimum === 'number' && maximum) {
w.writeLine(` ${propertyName}: ${isRequired ? `this._${propertyName} >= ${minimum} && this._${propertyName} <= ${maximum}` : `!this._${propertyName} ? true : ((this._${propertyName} >= ${minimum}) && (this._${propertyName} <= ${maximum}))`},`);
w.writeLine(` ${sPropName}: ${isRequired ? `this._${sPropName} >= ${minimum} && this._${sPropName} <= ${maximum}` : `!this._${sPropName} ? true : ((this._${sPropName} >= ${minimum}) && (this._${sPropName} <= ${maximum}))`},`);
}
if (typeof minimum !== 'number' || !maximum) {
w.writeLine(` ${propertyName}: ${isRequired ? `typeof this._${propertyName} === 'number'` : `!this._${propertyName} ? true : typeof this._${propertyName} === 'number'`},`);
w.writeLine(` ${sPropName}: ${isRequired ? `typeof this._${sPropName} === 'number'` : `!this._${sPropName} ? true : typeof this._${sPropName} === 'number'`},`);
}
}
} else if (type === 'boolean') {
w.writeLine(` ${propertyName}: ${isRequired ? `typeof this._${propertyName} === 'boolean'` : `!this._${propertyName} ? true : typeof this._${propertyName} === 'boolean'`},`);
} else if (pType === 'boolean') {
w.writeLine(` ${sPropName}: ${isRequired ? `typeof this._${sPropName} === 'boolean'` : `!this._${sPropName} ? true : typeof this._${sPropName} === 'boolean'`},`);
}
}
});
w.writeLine('};');
w.writeLine('const errorInFields: string[] = [];')
w.writeLine('Object.keys(validateRequired).forEach((key) => {');
w.writeLine(' if (!(validateRequired as any)[key]) {');
w.writeLine(' errorInFields.push(key);');
w.writeLine('const isError: string[] = [];')
w.writeLine('Object.keys(validate).forEach((key) => {');
w.writeLine(' if (!(validate as any)[key]) {');
w.writeLine(' isError.push(key);');
w.writeLine(' }');
w.writeLine('});');
w.writeLine('return errorInFields;');
w.writeLine('return isError;');
});
// add update method;
const update = entityClass.addMethod({
isStatic: false,
name: 'update',
returnType: `${sName}`,
});
update.addParameter({
name: 'props',
type: additionalPropsOnly ? `I${sName}` : `Partial<I${sName}>`,
});
update.setBodyText((w) => { w.writeLine(`return new ${sName}({ ...this.serialize(), ...props });`); });
this.entities.push(entityFile);
};

View File

@@ -19,154 +19,65 @@ const TYPES = {
boolean: 'boolean',
};
export enum SchemaType {
STRING = 'string',
OBJECT = 'object',
ARRAY = 'array',
BOOLEAN = 'boolean',
NUMBER = 'number',
INTEGER = 'integer',
}
export interface Schema {
allOf?: any[];
example?: string;
properties?: Record<string, Schema>;
required?: string[];
description?: string;
enum?: string[];
type: SchemaType;
pattern?: string;
oneOf?: any
items?: Schema;
additionalProperties?: Schema;
$ref?: string;
minItems?: number;
maxItems?: number;
maxLength?: number;
minLength?: number;
maximum?: number;
minimum?: number;
}
export interface Parameter {
description?: string;
example?: string;
in?: 'query' | 'body' | 'headers';
name: string;
schema: Schema;
required?: boolean;
}
export interface RequestBody {
content: {
'application/json'?: {
schema: Schema;
example?: string;
};
}
required?: boolean;
}
export interface Response {
content: {
'application/json'?: {
schema: Schema;
example?: string;
};
'text/palin'?: {
example?: string;
'x-error-class'?: string;
'x-error-code'?: string;
}
}
description?: string;
}
export interface Schemas {
parameters: Record<string, Parameter>;
requestBodies: Record<string, RequestBody>;
responses: Record<string, Response>;
schemas: Record<string, Schema>;
}
export interface OpenApi {
components: Schemas;
paths: any;
servers: {
description: string;
url: string;
}[]
}
/**
* @param schemaProp: valueof shema.properties[key]
* @param openApi: openapi object
* @returns [propType - basicType or import one, isArray, isClass, isImport]
*/
interface SchemaParamParserReturn {
type: string;
isArray: boolean;
isClass: boolean;
isImport: boolean;
isAdditional: boolean;
isEnum: boolean;
}
const schemaParamParser = (schemaProp: Schema, openApi: OpenApi): SchemaParamParserReturn => {
const schemaParamParser = (schemaProp: any, openApi: any): [string, boolean, boolean, boolean, boolean] => {
let type = '';
let isImport = false;
let isClass = false;
let isArray = false;
let isAdditional = false;
let isEnum = false;
if (schemaProp.$ref || schemaProp.additionalProperties?.$ref) {
type = (schemaProp.$ref || schemaProp.additionalProperties?.$ref)!.split('/').pop()!;
const temp = (schemaProp.$ref || schemaProp.additionalProperties?.$ref).split('/');
if (schemaProp.additionalProperties) {
isAdditional = true;
}
const cl = openApi.components.schemas[type];
if (cl.allOf) {
const ref = cl.allOf.find((e) => !!e.$ref);
const link = schemaParamParser(ref, openApi);
return {...link, type};
}
type = `${temp[temp.length - 1]}`;
const cl = openApi ? openApi.components.schemas[type] : {};
if (cl.$ref) {
const link = schemaParamParser(cl, openApi);
return {...link, type};
link.shift();
return [type, ...link] as any;
}
if (cl.type === 'string' && cl.enum) {
isImport = true;
isEnum = true;
}
if (cl.type === 'object' && !cl.oneOf) {
isClass = true;
isImport = true;
} else if (cl.type === 'array') {
const temp = schemaParamParser(cl.items!, openApi);
type = temp.type;
const temp: any = schemaParamParser(cl.items, openApi);
type = `${temp[0]}`;
isArray = true;
isClass = isClass || temp.isClass;
isImport = isImport || temp.isImport;
isEnum = isEnum || temp.isEnum;
isClass = isClass || temp[2];
isImport = isImport || temp[3];
}
} else if (schemaProp.type === 'array') {
const temp = schemaParamParser(schemaProp.items!, openApi);
type = temp.type
const temp: any = schemaParamParser(schemaProp.items, openApi);
type = `${temp[0]}`;
isArray = true;
isClass = isClass || temp.isClass;
isImport = isImport || temp.isImport;
isEnum = isEnum || temp.isEnum;
isClass = isClass || temp[2];
isImport = isImport || temp[3];
} else {
type = (TYPES as Record<any, string>)[schemaProp.type];
}
if (!type) {
// TODO: Fix bug with Error fields.
type = 'any';
// throw new Error('Failed to find entity type');
}
return { type, isArray, isClass, isImport, isAdditional, isEnum };
return [type, isArray, isClass, isImport, isAdditional];
};
export { TYPES, toCamel, capitalize, uncapitalize, schemaParamParser };

View File

@@ -370,16 +370,17 @@
remark "^13.0.0"
unist-util-find-all-after "^3.0.2"
"@ts-morph/common@~0.8.0":
version "0.8.0"
resolved "https://registry.yarnpkg.com/@ts-morph/common/-/common-0.8.0.tgz#ae7b292df8258040465c50b378108ec8f09a9516"
integrity sha512-YbjWiMXLMKxWxcMqP47nwZVWVBwoF5B65dtRz0lya2LetjldAPxTxRbRo1n4Iszr2tSvzXeaa+f1AbULmfc5uA==
"@ts-morph/common@~0.6.0":
version "0.6.0"
resolved "https://registry.yarnpkg.com/@ts-morph/common/-/common-0.6.0.tgz#cbd4ee57c5ef971511b9c5778e0bb8eb27de4783"
integrity sha512-pI35nZz5bs3tL3btSVX2cWkAE8rc80F+Fn4TwSC6bQvn7fgn9IyLXVcAfpG6X6NBY5wN9TkSWXn/QYUkBvR/Fw==
dependencies:
"@dsherret/to-absolute-glob" "^2.0.2"
fast-glob "^3.2.5"
fast-glob "^3.2.4"
fs-extra "^9.0.1"
is-negated-glob "^1.0.0"
mkdirp "^1.0.4"
multimatch "^5.0.0"
multimatch "^4.0.0"
typescript "~4.0.2"
"@types/anymatch@*":
version "1.3.1"
@@ -1171,6 +1172,11 @@ async@^2.6.2:
dependencies:
lodash "^4.17.14"
at-least-node@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/at-least-node/-/at-least-node-1.0.0.tgz#602cd4b46e844ad4effc92a8011a3c46e0238dc2"
integrity sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==
atob@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
@@ -1587,7 +1593,7 @@ coa@^2.0.2:
chalk "^2.4.1"
q "^1.1.2"
code-block-writer@^10.1.1:
code-block-writer@^10.1.0:
version "10.1.1"
resolved "https://registry.yarnpkg.com/code-block-writer/-/code-block-writer-10.1.1.tgz#ad5684ed4bfb2b0783c8b131281ae84ee640a42f"
integrity sha512-67ueh2IRGst/51p0n6FvPrnRjAGHY5F8xdjkgrYE7DDzpJe6qA07RYQ9VcoUeo5ATOjSOiWpSL3SWBRRbempMw==
@@ -2904,9 +2910,9 @@ fastest-levenshtein@^1.0.12:
integrity sha512-On2N+BpYJ15xIC974QNVuYGMOlEVt4s0EOI3wwMqOmK1fdDY+FN/zltPV8vosq4ad4c/gJ1KHScUn/6AWIgiow==
fastq@^1.6.0:
version "1.11.0"
resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.11.0.tgz#bb9fb955a07130a918eb63c1f5161cc32a5d0858"
integrity sha512-7Eczs8gIPDrVzT+EksYBcupqMyxSHXXrHOLRRxU2/DicV8789MRBRR8+Hc2uWzUupOs4YS4JzBmBxjjCVBxD/g==
version "1.10.0"
resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.10.0.tgz#74dbefccade964932cdf500473ef302719c652bb"
integrity sha512-NL2Qc5L3iQEsyYzweq7qfgy5OtXCmGzGvhElGEd/SoFWEMOEczNh5s5ocaF01HDetxz+p8ecjNPA6cZxxIHmzA==
dependencies:
reusify "^1.0.4"
@@ -3078,6 +3084,16 @@ fs-extra@^8.1.0:
jsonfile "^4.0.0"
universalify "^0.1.0"
fs-extra@^9.0.1:
version "9.1.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.1.0.tgz#5954460c764a8da2094ba3554bf839e6b9a7c86d"
integrity sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==
dependencies:
at-least-node "^1.0.0"
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
universalify "^2.0.0"
fs-minipass@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-2.1.0.tgz#7f5036fdbf12c63c169190cbe4199c852271f9fb"
@@ -3251,16 +3267,11 @@ gonzales-pe@^4.3.0:
dependencies:
minimist "^1.2.5"
graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.2.4:
graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.2.0, graceful-fs@^4.2.4:
version "4.2.4"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.4.tgz#2256bde14d3632958c465ebc96dc467ca07a29fb"
integrity sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw==
graceful-fs@^4.1.6, graceful-fs@^4.2.0:
version "4.2.6"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.6.tgz#ff040b2b0853b23c3d31027523706f1885d76bee"
integrity sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==
handle-thing@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/handle-thing/-/handle-thing-2.0.1.tgz#857f79ce359580c340d43081cc648970d0bb234e"
@@ -4134,6 +4145,15 @@ jsonfile@^4.0.0:
optionalDependencies:
graceful-fs "^4.1.6"
jsonfile@^6.0.1:
version "6.1.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.1.0.tgz#bc55b2634793c679ec6403094eb13698a6ec0aae"
integrity sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==
dependencies:
universalify "^2.0.0"
optionalDependencies:
graceful-fs "^4.1.6"
"jsx-ast-utils@^2.4.1 || ^3.0.0":
version "3.2.0"
resolved "https://registry.yarnpkg.com/jsx-ast-utils/-/jsx-ast-utils-3.2.0.tgz#41108d2cec408c3453c1bbe8a4aae9e1e2bd8f82"
@@ -4764,10 +4784,10 @@ multicast-dns@^6.0.1:
dns-packet "^1.3.1"
thunky "^1.0.2"
multimatch@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/multimatch/-/multimatch-5.0.0.tgz#932b800963cea7a31a033328fa1e0c3a1874dbe6"
integrity sha512-ypMKuglUrZUD99Tk2bUQ+xNQj43lPEfAeX2o9cTteAmShXy2VHDJpuwu1o0xqoKCt9jLVAvwyFKdLTPXKAfJyA==
multimatch@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/multimatch/-/multimatch-4.0.0.tgz#8c3c0f6e3e8449ada0af3dd29efb491a375191b3"
integrity sha512-lDmx79y1z6i7RNx0ZGCPq1bzJ6ZoDDKbvh7jxr9SJcWLkShMzXrHbYVpTdnhNM5MXpDUxCQ4DgqVttVXlBgiBQ==
dependencies:
"@types/minimatch" "^3.0.3"
array-differ "^3.0.0"
@@ -6272,11 +6292,6 @@ querystringify@^2.1.1:
resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.2.0.tgz#3345941b4153cb9d082d8eee4cda2016a9aef7f6"
integrity sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==
queue-microtask@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.2.tgz#abf64491e6ecf0f38a6502403d4cda04f372dfd3"
integrity sha512-dB15eXv3p2jDlbOiNLyMabYg1/sXvppd8DP2J3EOCQ0AkuSXCW2tP7mnVouVLJKgUMY6yP0kcQDVpLCN13h4Xg==
quick-lru@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-4.0.1.tgz#5b8878f113a58217848c6482026c73e1ba57727f"
@@ -7071,11 +7086,9 @@ rimraf@^3.0.2:
glob "^7.1.3"
run-parallel@^1.1.9:
version "1.2.0"
resolved "https://registry.yarnpkg.com/run-parallel/-/run-parallel-1.2.0.tgz#66d1368da7bdf921eb9d95bd1a9229e7f21a43ee"
integrity sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==
dependencies:
queue-microtask "^1.2.2"
version "1.1.10"
resolved "https://registry.yarnpkg.com/run-parallel/-/run-parallel-1.1.10.tgz#60a51b2ae836636c81377df16cb107351bcd13ef"
integrity sha512-zb/1OuZ6flOlH6tQyMPUrE3x3Ulxjlo9WIVXR4yVYi4H9UXQaeIsPbLn2R3O3vQCnDKkAl2qHiuocKKX4Tz/Sw==
safe-buffer@5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
version "5.1.2"
@@ -7929,14 +7942,14 @@ ts-loader@^8.0.6:
micromatch "^4.0.0"
semver "^7.3.4"
ts-morph@^10.0.1:
version "10.0.1"
resolved "https://registry.yarnpkg.com/ts-morph/-/ts-morph-10.0.1.tgz#5a620cc4ef85e3e6d161989e690f44d0a0f723b0"
integrity sha512-T1zufImtp5goTLTFhzi7XuKR1y/f+Jwz1gSULzB045LFjXuoqVlR87sfkpyWow8u2JwgusCJrhOnwmHCFNutTQ==
ts-morph@^8.1.2:
version "8.2.0"
resolved "https://registry.yarnpkg.com/ts-morph/-/ts-morph-8.2.0.tgz#41d83cd501cbd897eb029ac489d6d5b927555c57"
integrity sha512-NHHWu+7I2/AOZiTni5w3f+xCfIxrkzPCcQbTGa81Yk3pr23a4h9xLLEE6tIGuYIubWjkjr9QVC3ITqgmA5touQ==
dependencies:
"@dsherret/to-absolute-glob" "^2.0.2"
"@ts-morph/common" "~0.8.0"
code-block-writer "^10.1.1"
"@ts-morph/common" "~0.6.0"
code-block-writer "^10.1.0"
ts-node@^9.0.0:
version "9.1.1"
@@ -8019,6 +8032,11 @@ typescript@^4.0.3:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==
typescript@~4.0.2:
version "4.0.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389"
integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ==
unc-path-regex@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/unc-path-regex/-/unc-path-regex-0.1.2.tgz#e73dd3d7b0d7c5ed86fbac6b0ae7d8c6a69d50fa"
@@ -8094,6 +8112,11 @@ universalify@^0.1.0:
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==
universalify@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717"
integrity sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==
unpipe@1.0.0, unpipe@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"

2
go.mod
View File

@@ -3,7 +3,7 @@ module github.com/AdguardTeam/AdGuardHome
go 1.14
require (
github.com/AdguardTeam/dnsproxy v0.33.9
github.com/AdguardTeam/dnsproxy v0.35.5
github.com/AdguardTeam/golibs v0.4.4
github.com/AdguardTeam/urlfilter v0.14.3
github.com/NYTimes/gziphandler v1.1.1

4
go.sum
View File

@@ -18,8 +18,8 @@ dmitri.shuralyov.com/html/belt v0.0.0-20180602232347-f7d459c86be0/go.mod h1:JLBr
dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1:a1inKt/atXimZ4Mv927x+r7UpyzRUf4emIoiiSC2TN4=
dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU=
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/AdguardTeam/dnsproxy v0.33.9 h1:HUwywkhUV/M73E7qWcBAF+SdsNq742s82Lvox4pr/tM=
github.com/AdguardTeam/dnsproxy v0.33.9/go.mod h1:dkI9VWh43XlOzF2XogDm1EmoVl7PANOR4isQV6X9LZs=
github.com/AdguardTeam/dnsproxy v0.35.5 h1:SsRF0eDzuLGaSUDKABIu9Mn1joi4v4kvEU1vju2DQPQ=
github.com/AdguardTeam/dnsproxy v0.35.5/go.mod h1:dkI9VWh43XlOzF2XogDm1EmoVl7PANOR4isQV6X9LZs=
github.com/AdguardTeam/golibs v0.4.0/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4=
github.com/AdguardTeam/golibs v0.4.2 h1:7M28oTZFoFwNmp8eGPb3ImmYbxGaJLyQXeIFVHjME0o=
github.com/AdguardTeam/golibs v0.4.2/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4=

View File

@@ -7,6 +7,7 @@ import (
"encoding/binary"
"fmt"
"net"
"strings"
"sync"
"time"
@@ -463,7 +464,16 @@ func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (*Lease, bool) {
}
if lease.Expiry.Unix() != leaseExpireStatic {
lease.Hostname = string(hostname)
// The trimming is required since some devices include trailing
// zero-byte in DHCP option length calculation.
//
// See https://github.com/AdguardTeam/AdGuardHome/issues/2582.
//
// TODO(e.burkov): Remove after the trimming for hostname option
// will be added into github.com/insomniacslk/dhcp module.
hostnameStr := strings.TrimRight(string(hostname), "\x00")
lease.Hostname = hostnameStr
s.commitLease(lease)
} else if len(lease.Hostname) != 0 {
o := &optFQDN{

View File

@@ -276,14 +276,24 @@ func (s *Server) prepareUpstreamSettings() error {
upstreams = s.conf.UpstreamDNS
}
upstreams = filterOutComments(upstreams)
upstreamConfig, err := proxy.ParseUpstreamsConfig(upstreams, s.conf.BootstrapDNS, DefaultTimeout)
upstreamConfig, err := proxy.ParseUpstreamsConfig(upstreams,
upstream.Options{
Bootstrap: s.conf.BootstrapDNS,
Timeout: DefaultTimeout,
},
)
if err != nil {
return fmt.Errorf("dns: proxy.ParseUpstreamsConfig: %w", err)
}
if len(upstreamConfig.Upstreams) == 0 {
log.Info("warning: no default upstream servers specified, using %v", defaultDNS)
uc, err := proxy.ParseUpstreamsConfig(defaultDNS, s.conf.BootstrapDNS, DefaultTimeout)
uc, err := proxy.ParseUpstreamsConfig(defaultDNS,
upstream.Options{
Bootstrap: s.conf.BootstrapDNS,
Timeout: DefaultTimeout,
},
)
if err != nil {
return fmt.Errorf("dns: failed to parse default upstreams: %v", err)
}

View File

@@ -150,7 +150,7 @@ func (req *dnsConfig) checkBootstrap() (string, error) {
return boot, fmt.Errorf("invalid bootstrap server address: empty")
}
if _, err := upstream.NewResolver(boot, 0); err != nil {
if _, err := upstream.NewResolver(boot, upstream.Options{Timeout: 0}); err != nil {
return boot, fmt.Errorf("invalid bootstrap server address: %w", err)
}
}
@@ -315,7 +315,12 @@ func ValidateUpstreams(upstreams []string) error {
return nil
}
_, err := proxy.ParseUpstreamsConfig(upstreams, []string{}, DefaultTimeout)
_, err := proxy.ParseUpstreamsConfig(upstreams,
upstream.Options{
Bootstrap: []string{},
Timeout: DefaultTimeout,
},
)
if err != nil {
return err
}

View File

@@ -2,13 +2,10 @@ package home
import (
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"math"
"math/big"
"net/http"
"strings"
"sync"
@@ -20,8 +17,12 @@ import (
)
const (
cookieTTL = 365 * 24 // in hours
// cookieTTL is given in hours.
cookieTTL = 365 * 24
sessionCookieName = "agh_session"
// sessionTokenSize is the length of session token in bytes.
sessionTokenSize = 16
)
type session struct {
@@ -285,16 +286,29 @@ type loginJSON struct {
Password string `json:"password"`
}
func getSession(u *User) ([]byte, error) {
maxSalt := big.NewInt(math.MaxUint32)
salt, err := rand.Int(rand.Reader, maxSalt)
// newSessionToken returns cryptographically secure randomly generated slice of
// bytes of sessionTokenSize length.
//
// TODO(e.burkov): Think about using byte array instead of byte slice.
func newSessionToken() (data []byte, err error) {
randData := make([]byte, sessionTokenSize)
_, err = rand.Read(randData)
if err != nil {
return nil, err
}
d := []byte(fmt.Sprintf("%s%s%s", salt, u.Name, u.PasswordHash))
hash := sha256.Sum256(d)
return hash[:], nil
return randData, nil
}
// cookieTimeFormat is the format to be used in (time.Time).Format for cookie's
// expiry field.
const cookieTimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
// cookieExpiryFormat returns the formatted exp to be used in cookie string.
// It's quite simple for now, but probably will be expanded in the future.
func cookieExpiryFormat(exp time.Time) (formatted string) {
return exp.Format(cookieTimeFormat)
}
func (a *Auth) httpCookie(req loginJSON) (string, error) {
@@ -303,24 +317,23 @@ func (a *Auth) httpCookie(req loginJSON) (string, error) {
return "", nil
}
sess, err := getSession(&u)
sess, err := newSessionToken()
if err != nil {
return "", err
}
now := time.Now().UTC()
expire := now.Add(cookieTTL * time.Hour)
expstr := expire.Format(time.RFC1123)
expstr = expstr[:len(expstr)-len("UTC")] // "UTC" -> "GMT"
expstr += "GMT"
s := session{}
s.userName = u.Name
s.expire = uint32(now.Unix()) + a.sessionTTL
a.addSession(sess, &s)
a.addSession(sess, &session{
userName: u.Name,
expire: uint32(now.Unix()) + a.sessionTTL,
})
return fmt.Sprintf("%s=%s; Path=/; HttpOnly; Expires=%s",
sessionCookieName, hex.EncodeToString(sess), expstr), nil
return fmt.Sprintf(
"%s=%s; Path=/; HttpOnly; Expires=%s",
sessionCookieName, hex.EncodeToString(sess),
cookieExpiryFormat(now.Add(cookieTTL*time.Hour)),
), nil
}
func handleLogin(w http.ResponseWriter, r *http.Request) {

View File

@@ -1,6 +1,8 @@
package home
import (
"bytes"
"crypto/rand"
"encoding/hex"
"net/http"
"net/url"
@@ -11,6 +13,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
@@ -24,14 +27,34 @@ func prepareTestDir() string {
return dir
}
func TestNewSessionToken(t *testing.T) {
// Successful case.
token, err := newSessionToken()
require.Nil(t, err)
assert.Len(t, token, sessionTokenSize)
// Break the rand.Reader.
prevReader := rand.Reader
t.Cleanup(func() {
rand.Reader = prevReader
})
rand.Reader = &bytes.Buffer{}
// Unsuccessful case.
token, err = newSessionToken()
require.NotNil(t, err)
assert.Empty(t, token)
}
func TestAuth(t *testing.T) {
dir := prepareTestDir()
defer func() { _ = os.RemoveAll(dir) }()
t.Cleanup(func() { _ = os.RemoveAll(dir) })
fn := filepath.Join(dir, "sessions.db")
users := []User{
{Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"},
}
users := []User{{
Name: "name",
PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2",
}}
a := InitAuth(fn, nil, 60)
s := session{}
@@ -41,7 +64,7 @@ func TestAuth(t *testing.T) {
assert.Equal(t, checkSessionNotFound, a.checkSession("notfound"))
a.RemoveSession("notfound")
sess, err := getSession(&users[0])
sess, err := newSessionToken()
assert.Nil(t, err)
sessStr := hex.EncodeToString(sess)

View File

@@ -17,6 +17,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
"github.com/AdguardTeam/AdGuardHome/internal/util"
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/utils"
)
@@ -295,7 +296,12 @@ func (clients *clientsContainer) FindUpstreams(ip string) *proxy.UpstreamConfig
}
if c.upstreamConfig == nil {
config, err := proxy.ParseUpstreamsConfig(c.Upstreams, config.DNS.BootstrapDNS, dnsforward.DefaultTimeout)
config, err := proxy.ParseUpstreamsConfig(c.Upstreams,
upstream.Options{
Bootstrap: config.DNS.BootstrapDNS,
Timeout: dnsforward.DefaultTimeout,
},
)
if err == nil {
c.upstreamConfig = &config
}

View File

@@ -146,7 +146,7 @@ func TestHome(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode)
// test DNS over UDP
r, err := upstream.NewResolver("127.0.0.1:5354", 3*time.Second)
r, err := upstream.NewResolver("127.0.0.1:5354", upstream.Options{Timeout: 3 * time.Second})
assert.Nil(t, err)
addrs, err := r.LookupIPAddr(context.TODO(), "static.adguard.com")
assert.Nil(t, err)

View File

@@ -21,7 +21,7 @@ type onChangedT func()
// AutoHosts - automatic DNS records
type AutoHosts struct {
// lock protects table and tableReverse.
lock sync.Mutex
lock sync.RWMutex
// table is the host-to-IPs map.
table map[string][]net.IP
// tableReverse is the IP-to-hosts map.
@@ -119,15 +119,14 @@ func (a *AutoHosts) Process(host string, qtype uint16) []net.IP {
}
var ipsCopy []net.IP
a.lock.Lock()
a.lock.RLock()
defer a.lock.RUnlock()
if ips, ok := a.table[host]; ok {
ipsCopy = make([]net.IP, len(ips))
copy(ipsCopy, ips)
}
a.lock.Unlock()
log.Debug("AutoHosts: answer: %s -> %v", host, ipsCopy)
return ipsCopy
}
@@ -145,8 +144,8 @@ func (a *AutoHosts) ProcessReverse(addr string, qtype uint16) (hosts []string) {
ipStr := ipReal.String()
a.lock.Lock()
defer a.lock.Unlock()
a.lock.RLock()
defer a.lock.RUnlock()
hosts = a.tableReverse[ipStr]
@@ -161,8 +160,8 @@ func (a *AutoHosts) ProcessReverse(addr string, qtype uint16) (hosts []string) {
// List returns an IP-to-hostnames table. It is safe for concurrent use.
func (a *AutoHosts) List() (ipToHosts map[string][]string) {
a.lock.Lock()
defer a.lock.Unlock()
a.lock.RLock()
defer a.lock.RUnlock()
ipToHosts = make(map[string][]string, len(a.tableReverse))
for k, v := range a.tableReverse {
@@ -339,10 +338,13 @@ func (a *AutoHosts) updateHosts() {
}
}
a.lock.Lock()
a.table = table
a.tableReverse = tableRev
a.lock.Unlock()
func() {
a.lock.Lock()
defer a.lock.Unlock()
a.table = table
a.tableReverse = tableRev
}()
a.notify()
}

View File

@@ -5,10 +5,12 @@
package util
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
@@ -64,16 +66,43 @@ func SplitNext(str *string, splitBy byte) string {
return strings.TrimSpace(s)
}
// IsOpenWRT checks if OS is OpenWRT.
// IsOpenWRT returns true if host OS is OpenWRT.
func IsOpenWRT() bool {
if runtime.GOOS != "linux" {
return false
}
body, err := ioutil.ReadFile("/etc/os-release")
const etcDir = "/etc"
// TODO(e.burkov): Take care of dealing with fs package after updating
// Go version to 1.16.
fileInfos, err := ioutil.ReadDir(etcDir)
if err != nil {
return false
}
return strings.Contains(string(body), "OpenWrt")
// fNameSubstr is a part of a name of the desired file.
const fNameSubstr = "release"
osNameData := []byte("OpenWrt")
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() {
continue
}
if !strings.Contains(fileInfo.Name(), fNameSubstr) {
continue
}
body, err := ioutil.ReadFile(filepath.Join(etcDir, fileInfo.Name()))
if err != nil {
continue
}
if bytes.Contains(body, osNameData) {
return true
}
}
return false
}

View File

@@ -1279,6 +1279,8 @@
'type': 'string'
'edns_cs_enabled':
'type': 'boolean'
'disable_ipv6':
'type': 'boolean'
'dnssec_enabled':
'type': 'boolean'
'cache_size':

View File

@@ -11,8 +11,10 @@ set -e -f -u
readonly awk_program='/^v[0-9]+\.[0-9]+\.[0-9]+.*$/ {
if (!$4) {
# The last tag is a full release version, so bump the
# minor one to get the next one.
# minor release number and zero the patch release number
# to get the next release.
$2++;
$3 = 0;
}
print($1 "." $2 "." $3);