Commit ca9f36ef by Finley Ge Committed by GitHub

chore: Jest Testing structure (#2707)

* deps: add jest deps

* chore: mock

* feat: use mocinggoose

* feat: jest

* chore: remove babel.config.js
parent 26543479
......@@ -7,3 +7,7 @@ export type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Omit<T, Keys> &
{
[K in Keys]-?: Required<Pick<T, K>> & Partial<Record<Exclude<Keys, K>, undefined>>;
}[Keys];
export type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
/**
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/
const esModules = ['nanoid'].join('|');
/** @type {import('jest').Config} */
const config = {
// All imported modules in your tests should be mocked automatically
// automock: false,
// Stop running tests after `n` failures
// bail: 0,
// The directory where Jest should store its cached dependency information
// cacheDirectory: "/tmp/jest_rs",
// Automatically clear mock calls, instances, contexts and results before every test
// clearMocks: false,
// Indicates whether the coverage information should be collected while executing the test
collectCoverage: true,
// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,
// The directory where Jest should output its coverage files
coverageDirectory: './tmp/coverage',
// An array of regexp pattern strings used to skip coverage collection
coveragePathIgnorePatterns: ['/node_modules/', '/__mocks__/', '/src/test/'],
// Indicates which provider should be used to instrument code for coverage
// coverageProvider: "babel",
// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
// "text",
// "lcov",
// "clover"
// ],
// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,
// A path to a custom dependency extractor
// dependencyExtractor: undefined,
// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,
// The default configuration for fake timers
// fakeTimers: {
// "enableGlobally": false
// },
// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],
// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: undefined,
// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: undefined,
// A set of global variables that need to be available in all test environments
// globals: {},
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",
// An array of directory names to be searched recursively up from the requiring module's location
moduleDirectories: ['node_modules', 'src'],
// An array of file extensions your modules use
// moduleFileExtensions: ['js', 'mjs', 'cjs', 'jsx', 'ts', 'tsx', 'json', 'node'],
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: {
'@/(.*)': '<rootDir>/src/$1',
'^nanoid(/(.*)|$)': 'nanoid$1'
},
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
// Activates notifications for test results
// notify: false,
// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "failure-change",
// A preset that is used as a base for Jest's configuration
preset: 'ts-jest',
// Run tests from one or more projects
// projects: undefined,
// Use this configuration option to add custom reporters to Jest
// reporters: undefined,
// Automatically reset mock state before every test
// resetMocks: false,
// Reset the module registry before running each individual test
// resetModules: false,
// A path to a custom resolver
// resolver: undefined,
// Automatically restore mock state and implementation before every test
// restoreMocks: false,
// The root directory that Jest should scan for tests and modules within
// rootDir: undefined,
// A list of paths to directories that Jest should use to search for files in
// roots: [
// "<rootDir>"
// ],
// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",
// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],
// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],
// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],
// The test environment that will be used for testing
testEnvironment: 'node',
// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},
// Adds a location field to test results
// testLocationInResults: false,
// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// ],
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: ['/node_modules/'],
// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],
// This option allows the use of a custom results processor
// testResultsProcessor: undefined,
// This option allows use of a custom test runner
// testRunner: "jest-circus/runner",
// A map from regular expressions to paths to transformers
// transform: undefined,
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
transformIgnorePatterns: [`/node_modules/(?!${esModules})`]
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,
// Indicates whether each individual test should be reported during the run
// verbose: undefined,
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],
// Whether to use watchman for file crawling
// watchman: true,
};
module.exports = config;
......@@ -6,7 +6,8 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"test": "jest"
},
"dependencies": {
"@bany/curl-to-json": "^1.2.8",
......@@ -25,6 +26,7 @@
"@fortaine/fetch-event-source": "^3.0.6",
"@node-rs/jieba": "1.10.0",
"@tanstack/react-query": "^4.24.10",
"@types/jest": "^29.5.2",
"@types/nprogress": "^0.2.0",
"ahooks": "^3.7.11",
"axios": "^1.5.1",
......@@ -37,6 +39,7 @@
"hyperdown": "^2.4.29",
"i18next": "23.11.5",
"immer": "^9.0.19",
"jest": "^29.5.0",
"js-yaml": "^4.1.0",
"json5": "^2.2.3",
"jsonwebtoken": "^9.0.2",
......@@ -62,10 +65,12 @@
"remark-math": "^6.0.0",
"request-ip": "^3.3.0",
"sass": "^1.58.3",
"ts-jest": "^29.1.0",
"use-context-selector": "^1.4.4",
"zustand": "^4.3.5"
},
"devDependencies": {
"@shelf/jest-mongodb": "^4.3.2",
"@svgr/webpack": "^6.5.1",
"@types/formidable": "^2.0.5",
"@types/js-yaml": "^4.0.9",
......@@ -78,6 +83,8 @@
"@types/request-ip": "^0.0.37",
"eslint": "8.56.0",
"eslint-config-next": "14.2.3",
"mockingoose": "^2.16.2",
"mongodb-memory-server": "^10.0.0",
"nextjs-node-loader": "^1.1.5",
"typescript": "^5.1.3"
}
......
import { MongoMemoryServer } from 'mongodb-memory-server';
import mongoose from 'mongoose';
import { MockParseHeaderCert } from '@/test/utils';
import { initMockData } from './db/init';
jest.mock('nanoid', () => {
return {
nanoid: () => {}
};
});
jest.mock('@fastgpt/global/common/string/tools', () => {
return {
hashStr(str: string) {
return str;
}
};
});
jest.mock('@fastgpt/service/common/system/log', jest.fn());
jest.mock('@fastgpt/service/support/permission/controller', () => {
return {
parseHeaderCert: MockParseHeaderCert,
getResourcePermission: jest.requireActual('@fastgpt/service/support/permission/controller')
.getResourcePermission,
getResourceAllClbs: jest.requireActual('@fastgpt/service/support/permission/controller')
.getResourceAllClbs
};
});
const parse = jest.createMockFromModule('@fastgpt/service/support/permission/controller') as any;
parse.parseHeaderCert = MockParseHeaderCert;
jest.mock('@/service/middleware/entry', () => {
return {
NextAPI: (...args: any) => {
return async function api(req: any, res: any) {
try {
let response = null;
for (const handler of args) {
response = await handler(req, res);
}
return {
code: 200,
data: response
};
} catch (error) {
return {
code: 500,
error
};
}
};
}
};
});
beforeAll(async () => {
if (!global.mongod || !global.mongodb) {
const mongod = await MongoMemoryServer.create();
global.mongod = mongod;
global.mongodb = mongoose;
await global.mongodb.connect(mongod.getUri());
await initMockData();
}
});
afterAll(async () => {
if (global.mongodb) {
await global.mongodb.disconnect();
}
if (global.mongod) {
await global.mongod.stop();
}
});
import { TeamMemberRoleEnum } from '@fastgpt/global/support/user/team/constant';
import { MongoApp } from '@fastgpt/service/core/app/schema';
import { MongoUser } from '@fastgpt/service/support/user/schema';
import { MongoTeamMember } from '@fastgpt/service/support/user/team/teamMemberSchema';
import { MongoTeam } from '@fastgpt/service/support/user/team/teamSchema';
export const root = {
uid: '',
tmbId: '',
teamId: '',
isRoot: true,
appId: ''
};
export const initMockData = async () => {
// init root user
const rootUser = await MongoUser.create({
username: 'root',
password: '123456'
});
const rootTeam = await MongoTeam.create({
name: 'root-default-team',
ownerId: rootUser._id
});
const rootTeamMember = await MongoTeamMember.create({
teamId: rootTeam._id,
userId: rootUser._id,
name: 'root-default-team-member',
status: 'active',
role: TeamMemberRoleEnum.owner
});
const rootApp = await MongoApp.create({
name: 'root-default-app',
teamId: rootTeam._id,
tmbId: rootTeam._id,
type: 'advanced'
});
root.uid = rootUser._id;
root.tmbId = rootTeamMember._id;
root.teamId = rootTeam._id;
root.appId = rootApp._id;
await Promise.all([rootUser.save(), rootTeam.save(), rootTeamMember.save(), rootApp.save()]);
};
import { MongoMemoryServer } from 'mongodb-memory-server';
declare global {
var mongod: MongoMemoryServer | undefined;
}
import '../../__mocks__/base';
import { root } from '../../__mocks__/db/init';
import { getTestRequest } from '@/test/utils';
import type { OutLinkListQuery } from './list';
import { AppErrEnum } from '@fastgpt/global/common/error/code/app';
import handler from './list';
import { MongoOutLink } from '@fastgpt/service/support/outLink/schema';
beforeAll(async () => {
await MongoOutLink.create({
shareId: 'aaa',
appId: root.appId,
tmbId: root.tmbId,
teamId: root.teamId,
type: 'share',
name: 'aaa'
});
await MongoOutLink.create({
shareId: 'bbb',
appId: root.appId,
tmbId: root.tmbId,
teamId: root.teamId,
type: 'share',
name: 'bbb'
});
});
test('Should return a list of outLink', async () => {
const res = (await handler(
...getTestRequest<OutLinkListQuery>({
query: {
appId: root.appId,
type: 'share'
},
user: root
})
)) as any;
expect(res.code).toBe(200);
expect(res.data.length).toBe(2);
});
test('appId is required', async () => {
const res = (await handler(
...getTestRequest<OutLinkListQuery>({
query: {
type: 'share'
},
user: root
})
)) as any;
expect(res.code).toBe(500);
expect(res.error).toBe(AppErrEnum.unExist);
});
test('if type is not provided, return nothing', async () => {
const res = (await handler(
...getTestRequest<OutLinkListQuery>({
query: {
appId: root.appId
},
user: root
})
)) as any;
expect(res.code).toBe(200);
expect(res.data.length).toBe(0);
});
......@@ -4,6 +4,7 @@ import { ManagePermissionVal } from '@fastgpt/global/support/permission/constant
import type { ApiRequestProps } from '@fastgpt/service/type/next';
import { NextAPI } from '@/service/middleware/entry';
import { OutLinkSchema } from '@fastgpt/global/support/outLink/type';
import { PublishChannelEnum } from '@fastgpt/global/support/outLink/constant';
export const ApiMetadata = {
name: '获取应用内所有 Outlink',
......@@ -11,19 +12,18 @@ export const ApiMetadata = {
version: '0.1.0'
};
// Outlink
export type OutLinkListQuery = {
appId: string; // 应用 ID
type: string; // 类型
type: `${PublishChannelEnum}`;
};
export type OutLinkListBody = {};
// 响应: 应用内全部 Outlink
// 应用内全部 Outlink 列表
export type OutLinkListResponse = OutLinkSchema[];
// 查询应用内全部 Outlink
async function handler(
// 查询应用的所有 OutLink
export async function handler(
req: ApiRequestProps<OutLinkListBody, OutLinkListQuery>
): Promise<OutLinkListResponse> {
const { appId, type } = req.query;
......@@ -43,4 +43,5 @@ async function handler(
return data;
}
export default NextAPI(handler);
import { getTestRequest } from '@/test/utils';
import '../../__mocks__/base';
import handler, { OutLinkUpdateBody, OutLinkUpdateQuery } from './update';
import { root } from '../../__mocks__/db/init';
import { MongoOutLink } from '@fastgpt/service/support/outLink/schema';
import { CommonErrEnum } from '@fastgpt/global/common/error/code/common';
test('Update Outlink', async () => {
const outlink = await MongoOutLink.create({
shareId: 'aaa',
appId: root.appId,
tmbId: root.tmbId,
teamId: root.teamId,
type: 'share',
name: 'aaa'
});
await outlink.save();
const res = (await handler(
...getTestRequest<OutLinkUpdateQuery, OutLinkUpdateBody>({
body: {
_id: outlink._id,
name: 'changed'
},
user: root
})
)) as any;
expect(res.code).toBe(200);
const link = await MongoOutLink.findById(outlink._id).lean();
expect(link?.name).toBe('changed');
});
test('Did not post _id', async () => {
const res = (await handler(
...getTestRequest<OutLinkUpdateQuery, OutLinkUpdateBody>({
body: {
name: 'changed'
},
user: root
})
)) as any;
expect(res.code).toBe(500);
expect(res.error).toBe(CommonErrEnum.missingParams);
});
......@@ -7,7 +7,18 @@ import { NextAPI } from '@/service/middleware/entry';
import { CommonErrEnum } from '@fastgpt/global/common/error/code/common';
export type OutLinkUpdateQuery = {};
export type OutLinkUpdateBody = OutLinkEditType & {};
// {
// _id?: string; // Outlink 的 ID
// name: string; // Outlink 的名称
// responseDetail?: boolean; // 是否开启详细回复
// immediateResponse?: string; // 立即回复的内容
// defaultResponse?: string; // 默认回复的内容
// limit?: OutLinkSchema<T>['limit']; // 限制
// app?: T; // 平台的配置
// }
export type OutLinkUpdateBody = OutLinkEditType;
export type OutLinkUpdateResponse = {};
async function handler(
......
import { ERROR_ENUM } from '@fastgpt/global/common/error/errorCode';
export type TestTokenType = {
userId: string;
teamId: string;
tmbId: string;
isRoot: boolean;
};
export type TestRequest = {
headers: {
cookie?: {
token?: TestTokenType;
};
authorization?: string; // testkey
rootkey?: string; // rootkey
};
query: {
[key: string]: string;
};
body: {
[key: string]: string;
};
};
export function getTestRequest<Q = any, B = any>({
query = {},
body = {},
authToken = true,
// authRoot = false,
// authApiKey = false,
user
}: {
body?: Partial<B>;
query?: Partial<Q>;
authToken?: boolean;
authRoot?: boolean;
authApiKey?: boolean;
user?: {
uid: string;
tmbId: string;
teamId: string;
isRoot: boolean;
};
}): [any, any] {
const headers: TestRequest['headers'] = {};
if (authToken) {
headers.cookie = {
token: {
userId: String(user?.uid || ''),
teamId: String(user?.teamId || ''),
tmbId: String(user?.tmbId || ''),
isRoot: user?.isRoot || false
}
};
}
return [
{
headers,
query,
body
},
{}
];
}
export const MockParseHeaderCert = async ({
req,
authToken = true,
authRoot = false,
authApiKey = false
}: {
req: TestRequest;
authToken?: boolean;
authRoot?: boolean;
authApiKey?: boolean;
}): Promise<TestTokenType> => {
if (authToken) {
const token = req.headers?.cookie?.token;
if (!token) {
return Promise.reject(ERROR_ENUM.unAuthorization);
}
return token;
}
// if (authRoot) {
// // TODO: unfinished
// return req.headers.rootkey;
// }
// if (authApiKey) {
// // TODO: unfinished
// return req.headers.authorization;
// }
return {} as any;
};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("./utils");
var fs = require("fs");
var path = require("path");
var openapi_1 = require("./openapi");
var rootPath = 'projects/app/src/pages/api';
var exclude = ['/admin', '/proApi'];
function getAllFiles(dir) {
var files = [];
var stat = fs.statSync(dir);
if (stat.isDirectory()) {
var list = fs.readdirSync(dir);
list.forEach(function (item) {
var fullPath = path.join(dir, item);
if (!exclude.some(function (excluded) { return fullPath.includes(excluded); })) {
files = files.concat(getAllFiles(fullPath));
}
});
}
else {
files.push(dir);
}
return files;
}
var searchPath = process.env.SEARCH_PATH || '';
var files = getAllFiles(path.join(rootPath, searchPath));
// console.log(files)
var apis = files.map(function (file) {
return (0, utils_1.parseAPI)({ path: file, rootPath: rootPath });
});
var openapi = (0, openapi_1.convertOpenApi)({
apis: apis,
openapi: '3.0.0',
info: {
title: 'FastGPT OpenAPI',
version: '1.0.0',
author: 'FastGPT'
},
servers: [
{
url: 'http://localhost:4000'
}
]
});
var json = JSON.stringify(openapi, null, 2);
fs.writeFileSync('./scripts/openapi/openapi.json', json);
fs.writeFileSync('./scripts/openapi/openapi.out', JSON.stringify(apis, null, 2));
console.log('Total APIs:', files.length);
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertPath = convertPath;
exports.convertOpenApi = convertOpenApi;
function convertPath(api) {
var _a;
var _b;
var method = api.method.toLowerCase();
var parameters = [];
if (api.query) {
if (Array.isArray(api.query)) {
api.query.forEach(function (item) {
parameters.push({
name: item.key,
description: item.comment,
in: 'query',
required: item.required,
schema: {
type: item.type
}
});
});
}
else {
parameters.push({
description: api.query.comment,
name: api.query.key,
in: 'query',
required: api.query.required,
schema: {
type: api.query.type
}
});
}
}
else if (api.body) {
if (Array.isArray(api.body)) {
api.body.forEach(function (item) {
parameters.push({
description: item.comment,
name: item.key,
in: 'body',
required: item.required,
schema: {
type: item.type
}
});
});
}
}
var responses = (function () {
var _a, _b, _c;
if (api.response) {
if (Array.isArray(api.response)) {
var properties_1 = {};
api.response.forEach(function (item) {
var _a;
properties_1[item.type] = {
type: (_a = item.key) !== null && _a !== void 0 ? _a : item.type,
description: item.comment
};
});
var res = {
'200': {
description: (_a = api.description) !== null && _a !== void 0 ? _a : '',
content: {
'application/json': {
schema: {
type: 'object',
properties: properties_1
}
}
}
}
};
return res;
}
else {
return {
'200': {
description: (_b = api.response.comment) !== null && _b !== void 0 ? _b : '',
content: {
'application/json': {
schema: {
type: api.response.type
}
}
}
}
};
}
}
else {
return {
'200': {
description: (_c = api.description) !== null && _c !== void 0 ? _c : '',
content: {
'application/json': {
schema: {
type: 'object'
}
}
}
}
};
}
})();
return _a = {},
_a[method] = {
description: (_b = api.description) !== null && _b !== void 0 ? _b : '',
parameters: parameters,
responses: responses
},
_a;
}
function convertOpenApi(_a) {
var apis = _a.apis, rest = __rest(_a, ["apis"]);
var paths = {};
apis.forEach(function (api) {
paths[api.url] = convertPath(api);
});
return __assign({ paths: paths }, rest);
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseAPI = parseAPI;
var parser_1 = require("@babel/parser");
var traverse_1 = require("@babel/traverse");
var fs = require("fs");
function getMetadata(path) {
var _a, _b, _c;
var metadata = {
name: '',
author: '',
version: '',
method: ''
};
if (path.isExportNamedDeclaration() && // get metadata
((_a = path.node.declaration) === null || _a === void 0 ? void 0 : _a.type) === 'VariableDeclaration' &&
((_b = path.node.declaration.declarations[0]) === null || _b === void 0 ? void 0 : _b.id.type) === 'Identifier' &&
path.node.declaration.declarations[0].id.name === 'ApiMetadata' &&
((_c = path.node.declaration.declarations[0].init) === null || _c === void 0 ? void 0 : _c.type) === 'ObjectExpression') {
path.node.declaration.declarations[0].init.properties.forEach(function (item) {
if (item.type === 'ObjectProperty') {
var key = item.key.type === 'Identifier' ? item.key.name : item.key.type;
if (key === 'name') {
metadata.name = item.value.type === 'StringLiteral' ? item.value.value : item.value.type;
}
if (key === 'author') {
metadata.author =
item.value.type === 'StringLiteral' ? item.value.value : item.value.type;
}
if (key === 'version') {
metadata.version =
item.value.type === 'StringLiteral' ? item.value.value : item.value.type;
}
else if (key === 'method') {
metadata.method =
item.value.type === 'StringLiteral' ? item.value.value : item.value.type;
metadata.method = metadata.method.toUpperCase();
}
}
});
if (metadata.name && metadata.author && metadata.version) {
return metadata;
}
}
}
function getDescription(path) {
var _a, _b;
if (path.isFunctionDeclaration() && ((_a = path.node.id) === null || _a === void 0 ? void 0 : _a.name) === 'handler') {
var comments = (_b = path.node.leadingComments) === null || _b === void 0 ? void 0 : _b.map(function (item) { return item.value.trim(); }).join('\n');
return comments;
}
}
function parseType(type) {
if (!type) {
return '';
}
if (type.type === 'TSTypeReference') {
return type.typeName.type === 'Identifier' ? type.typeName.name : type.typeName.type;
}
else if (type.type === 'TSArrayType') {
return "".concat(parseType(type.elementType), "[]");
}
else if (type.type === 'TSUnionType') {
return type.types.map(function (item) { return parseType(item); }).join(' | ');
}
else if (type.type === 'TSIntersectionType') {
return type.types.map(function (item) { return parseType(item); }).join(' & ');
}
else if (type.type === 'TSLiteralType') {
return type.literal.type === 'StringLiteral' ? type.literal.value : type.literal.type;
// } else if (type.type === 'TSTypeLiteral') {
// return parseTypeLiteral(type);
}
else if (type.type === 'TSStringKeyword') {
return 'string';
}
else if (type.type === 'TSNumberKeyword') {
return 'number';
}
else if (type.type === 'TSBooleanKeyword') {
return 'boolean';
}
else {
return type.type;
}
}
function parseTypeLiteral(type) {
var items = [];
type.members.forEach(function (item) {
var _a, _b, _c;
if (item.type === 'TSPropertySignature') {
var key = item.key.type === 'Identifier' ? item.key.name : item.key.type;
var value = parseType((_a = item.typeAnnotation) === null || _a === void 0 ? void 0 : _a.typeAnnotation);
var comments = [
(_b = item.leadingComments) === null || _b === void 0 ? void 0 : _b.map(function (item) { return item.value.trim(); }).join('\n'),
(_c = item.trailingComments) === null || _c === void 0 ? void 0 : _c.map(function (item) { return item.value.trim(); }).join('\n')
].join('\n');
var required = item.optional ? false : true;
items.push({
type: value,
comment: comments,
key: key,
required: required
});
}
});
return items;
}
function getData(path) {
var _a, _b, _c;
var type = {};
if (path.isExportNamedDeclaration()) {
var comments = [
(_a = path.node.leadingComments) === null || _a === void 0 ? void 0 : _a.map(function (item) { return item.value.trim(); }).join('\n'),
(_b = path.node.trailingComments) === null || _b === void 0 ? void 0 : _b.map(function (item) { return item.value.trim(); }).join('\n')
].join('\n');
if (comments) {
type.comment = comments;
}
if (((_c = path.node.declaration) === null || _c === void 0 ? void 0 : _c.type) === 'TSTypeAliasDeclaration') {
if (path.node.declaration.id.type === 'Identifier') {
if (path.node.declaration.id.name.endsWith('Query')) {
type.type = 'query';
var queryType = path.node.declaration.typeAnnotation;
if (queryType) {
if (queryType.type === 'TSTypeLiteral') {
type.items = parseTypeLiteral(queryType);
}
else {
type.dataType = parseType(queryType);
}
}
}
else if (path.node.declaration.id.name.endsWith('Body')) {
type.type = 'body';
if (path.node.declaration.typeAnnotation) {
if (path.node.declaration.typeAnnotation.type === 'TSTypeLiteral') {
type.items = parseTypeLiteral(path.node.declaration.typeAnnotation);
}
else {
type.dataType = parseType(path.node.declaration.typeAnnotation);
}
}
}
else if (path.node.declaration.id.name.endsWith('Response')) {
type.type = 'response';
if (path.node.declaration.typeAnnotation) {
if (path.node.declaration.typeAnnotation.type === 'TSTypeLiteral') {
type.items = parseTypeLiteral(path.node.declaration.typeAnnotation);
}
else {
type.dataType = parseType(path.node.declaration.typeAnnotation);
}
}
}
else {
return;
}
}
}
}
return type;
}
function parseCode(code) {
var ast = (0, parser_1.parse)(code, {
sourceType: 'module',
plugins: ['typescript', 'jsx']
});
var api = {};
(0, traverse_1.default)(ast, {
enter: function (path) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
var metadata = getMetadata(path);
var description = getDescription(path);
var data = getData(path);
if (metadata) {
api.name = metadata.name;
api.author = metadata.author;
api.version = metadata.version;
}
if (description) {
api.description = description;
}
if (data) {
if (data.type === 'query') {
api.query = (_a = data.items) !== null && _a !== void 0 ? _a : {
type: (_b = data.dataType) !== null && _b !== void 0 ? _b : '',
comment: (_c = data.comment) !== null && _c !== void 0 ? _c : ''
};
}
else if (data.type === 'body') {
api.body = (_d = data.items) !== null && _d !== void 0 ? _d : {
type: (_e = data.dataType) !== null && _e !== void 0 ? _e : '',
comment: (_f = data.comment) !== null && _f !== void 0 ? _f : ''
};
}
else if (data.type === 'response') {
api.response = (_g = data.items) !== null && _g !== void 0 ? _g : {
type: (_h = data.dataType) !== null && _h !== void 0 ? _h : '',
comment: (_j = data.comment) !== null && _j !== void 0 ? _j : ''
};
}
}
}
});
return api;
}
function getMethod(api) {
if (api.query && !(Array.isArray(api.query) && api.query.length === 0)) {
return 'GET';
}
else if (api.body && !(Array.isArray(api.body) && api.body.length === 0)) {
return 'POST';
}
else {
return 'GET';
}
}
function parseAPI(_a) {
var path = _a.path, rootPath = _a.rootPath;
var code = fs.readFileSync(path, 'utf-8');
var api = parseCode(code);
api.url = path.replace('.ts', '').replace(rootPath, '');
api.path = path;
if (api.method === undefined) {
api.method = getMethod(api);
}
return api;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment