Convert `api.ts`, `api-implementation.ts` and `frames.ts` to ES6 modules (#5283)

This fixes ESLint errors in these files.

Set the `resolveJsonModule` setting, so that TypeScript can recognize the
`package.json` import.
main
Roman Donchenko 3 years ago committed by GitHub
parent 5dd7eff97a
commit c86746c785
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,329 +3,325 @@
// //
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
const config = require('./config').default; import config from './config';
(() => { import PluginRegistry from './plugins';
const PluginRegistry = require('./plugins').default; import serverProxy from './server-proxy';
const serverProxy = require('./server-proxy').default; import lambdaManager from './lambda-manager';
const lambdaManager = require('./lambda-manager').default; import {
const { isBoolean,
isBoolean, isInteger,
isInteger, isString,
isString, checkFilter,
checkFilter, checkExclusiveFields,
checkExclusiveFields, checkObjectType,
checkObjectType, } from './common';
} = require('./common');
import User from './user';
const User = require('./user').default; import { AnnotationFormats } from './annotation-formats';
const { AnnotationFormats } = require('./annotation-formats'); import { ArgumentError } from './exceptions';
const { ArgumentError } = require('./exceptions'); import { Task, Job } from './session';
const { Task, Job } = require('./session'); import Project from './project';
const Project = require('./project').default; import CloudStorage from './cloud-storage';
const CloudStorage = require('./cloud-storage').default; import Organization from './organization';
const Organization = require('./organization').default; import Webhook from './webhook';
const Webhook = require('./webhook').default;
export default function implementAPI(cvat) {
function implementAPI(cvat) { cvat.plugins.list.implementation = PluginRegistry.list;
cvat.plugins.list.implementation = PluginRegistry.list; cvat.plugins.register.implementation = PluginRegistry.register.bind(cvat);
cvat.plugins.register.implementation = PluginRegistry.register.bind(cvat);
cvat.lambda.list.implementation = lambdaManager.list.bind(lambdaManager);
cvat.lambda.list.implementation = lambdaManager.list.bind(lambdaManager); cvat.lambda.run.implementation = lambdaManager.run.bind(lambdaManager);
cvat.lambda.run.implementation = lambdaManager.run.bind(lambdaManager); cvat.lambda.call.implementation = lambdaManager.call.bind(lambdaManager);
cvat.lambda.call.implementation = lambdaManager.call.bind(lambdaManager); cvat.lambda.cancel.implementation = lambdaManager.cancel.bind(lambdaManager);
cvat.lambda.cancel.implementation = lambdaManager.cancel.bind(lambdaManager); cvat.lambda.listen.implementation = lambdaManager.listen.bind(lambdaManager);
cvat.lambda.listen.implementation = lambdaManager.listen.bind(lambdaManager); cvat.lambda.requests.implementation = lambdaManager.requests.bind(lambdaManager);
cvat.lambda.requests.implementation = lambdaManager.requests.bind(lambdaManager);
cvat.server.about.implementation = async () => {
cvat.server.about.implementation = async () => { const result = await serverProxy.server.about();
const result = await serverProxy.server.about(); return result;
return result; };
};
cvat.server.share.implementation = async (directory) => {
cvat.server.share.implementation = async (directory) => { const result = await serverProxy.server.share(directory);
const result = await serverProxy.server.share(directory); return result;
return result; };
};
cvat.server.formats.implementation = async () => {
cvat.server.formats.implementation = async () => { const result = await serverProxy.server.formats();
const result = await serverProxy.server.formats(); return new AnnotationFormats(result);
return new AnnotationFormats(result); };
};
cvat.server.userAgreements.implementation = async () => {
cvat.server.userAgreements.implementation = async () => { const result = await serverProxy.server.userAgreements();
const result = await serverProxy.server.userAgreements(); return result;
return result; };
};
cvat.server.register.implementation = async (
cvat.server.register.implementation = async ( username,
firstName,
lastName,
email,
password,
userConfirmations,
) => {
const user = await serverProxy.server.register(
username, username,
firstName, firstName,
lastName, lastName,
email, email,
password, password,
userConfirmations, userConfirmations,
) => { );
const user = await serverProxy.server.register(
username, return new User(user);
firstName, };
lastName,
email, cvat.server.login.implementation = async (username, password) => {
password, await serverProxy.server.login(username, password);
userConfirmations, };
);
cvat.server.logout.implementation = async () => {
return new User(user); await serverProxy.server.logout();
}; };
cvat.server.login.implementation = async (username, password) => { cvat.server.advancedAuthentication.implementation = async () => {
await serverProxy.server.login(username, password); const result = await serverProxy.server.advancedAuthentication();
}; return result;
};
cvat.server.logout.implementation = async () => {
await serverProxy.server.logout(); cvat.server.changePassword.implementation = async (oldPassword, newPassword1, newPassword2) => {
}; await serverProxy.server.changePassword(oldPassword, newPassword1, newPassword2);
};
cvat.server.advancedAuthentication.implementation = async () => {
const result = await serverProxy.server.advancedAuthentication(); cvat.server.requestPasswordReset.implementation = async (email) => {
return result; await serverProxy.server.requestPasswordReset(email);
}; };
cvat.server.changePassword.implementation = async (oldPassword, newPassword1, newPassword2) => { cvat.server.resetPassword.implementation = async (newPassword1, newPassword2, uid, token) => {
await serverProxy.server.changePassword(oldPassword, newPassword1, newPassword2); await serverProxy.server.resetPassword(newPassword1, newPassword2, uid, token);
}; };
cvat.server.requestPasswordReset.implementation = async (email) => { cvat.server.authorized.implementation = async () => {
await serverProxy.server.requestPasswordReset(email); const result = await serverProxy.server.authorized();
}; return result;
};
cvat.server.resetPassword.implementation = async (newPassword1, newPassword2, uid, token) => {
await serverProxy.server.resetPassword(newPassword1, newPassword2, uid, token); cvat.server.request.implementation = async (url, data) => {
}; const result = await serverProxy.server.request(url, data);
return result;
cvat.server.authorized.implementation = async () => { };
const result = await serverProxy.server.authorized();
return result; cvat.server.installedApps.implementation = async () => {
}; const result = await serverProxy.server.installedApps();
return result;
cvat.server.request.implementation = async (url, data) => { };
const result = await serverProxy.server.request(url, data);
return result; cvat.users.get.implementation = async (filter) => {
}; checkFilter(filter, {
id: isInteger,
cvat.server.installedApps.implementation = async () => { is_active: isBoolean,
const result = await serverProxy.server.installedApps(); self: isBoolean,
return result; search: isString,
}; limit: isInteger,
});
cvat.users.get.implementation = async (filter) => {
checkFilter(filter, { let users = null;
id: isInteger, if ('self' in filter && filter.self) {
is_active: isBoolean, users = await serverProxy.users.self();
self: isBoolean, users = [users];
search: isString, } else {
limit: isInteger, const searchParams = {};
}); for (const key in filter) {
if (filter[key] && key !== 'self') {
let users = null; searchParams[key] = filter[key];
if ('self' in filter && filter.self) {
users = await serverProxy.users.self();
users = [users];
} else {
const searchParams = {};
for (const key in filter) {
if (filter[key] && key !== 'self') {
searchParams[key] = filter[key];
}
} }
users = await serverProxy.users.get(searchParams);
} }
users = await serverProxy.users.get(searchParams);
users = users.map((user) => new User(user)); }
return users;
}; users = users.map((user) => new User(user));
return users;
cvat.jobs.get.implementation = async (filter) => { };
checkFilter(filter, {
page: isInteger, cvat.jobs.get.implementation = async (filter) => {
filter: isString, checkFilter(filter, {
sort: isString, page: isInteger,
search: isString, filter: isString,
taskID: isInteger, sort: isString,
jobID: isInteger, search: isString,
}); taskID: isInteger,
jobID: isInteger,
if ('taskID' in filter && 'jobID' in filter) { });
throw new ArgumentError('Filter fields "taskID" and "jobID" are not permitted to be used at the same time');
if ('taskID' in filter && 'jobID' in filter) {
throw new ArgumentError('Filter fields "taskID" and "jobID" are not permitted to be used at the same time');
}
if ('taskID' in filter) {
const [task] = await serverProxy.tasks.get({ id: filter.taskID });
if (task) {
return new Task(task).jobs;
} }
if ('taskID' in filter) { return [];
const [task] = await serverProxy.tasks.get({ id: filter.taskID }); }
if (task) {
return new Task(task).jobs;
}
return []; if ('jobID' in filter) {
const job = await serverProxy.jobs.get({ id: filter.jobID });
if (job) {
return [new Job(job)];
} }
}
if ('jobID' in filter) { const searchParams = {};
const job = await serverProxy.jobs.get({ id: filter.jobID }); for (const key of Object.keys(filter)) {
if (job) { if (['page', 'sort', 'search', 'filter'].includes(key)) {
return [new Job(job)]; searchParams[key] = filter[key];
}
}
const searchParams = {};
for (const key of Object.keys(filter)) {
if (['page', 'sort', 'search', 'filter'].includes(key)) {
searchParams[key] = filter[key];
}
} }
}
const jobsData = await serverProxy.jobs.get(searchParams);
const jobs = jobsData.results.map((jobData) => new Job(jobData)); const jobsData = await serverProxy.jobs.get(searchParams);
jobs.count = jobsData.count; const jobs = jobsData.results.map((jobData) => new Job(jobData));
return jobs; jobs.count = jobsData.count;
}; return jobs;
};
cvat.tasks.get.implementation = async (filter) => {
checkFilter(filter, { cvat.tasks.get.implementation = async (filter) => {
page: isInteger, checkFilter(filter, {
projectId: isInteger, page: isInteger,
id: isInteger, projectId: isInteger,
sort: isString, id: isInteger,
search: isString, sort: isString,
filter: isString, search: isString,
ordering: isString, filter: isString,
}); ordering: isString,
});
checkExclusiveFields(filter, ['id', 'projectId'], ['page']);
const searchParams = {}; checkExclusiveFields(filter, ['id', 'projectId'], ['page']);
for (const key of Object.keys(filter)) { const searchParams = {};
if (['page', 'id', 'sort', 'search', 'filter', 'ordering'].includes(key)) { for (const key of Object.keys(filter)) {
searchParams[key] = filter[key]; if (['page', 'id', 'sort', 'search', 'filter', 'ordering'].includes(key)) {
} searchParams[key] = filter[key];
} }
}
let tasksData = null; let tasksData = null;
if (filter.projectId) { if (filter.projectId) {
if (searchParams.filter) { if (searchParams.filter) {
const parsed = JSON.parse(searchParams.filter); const parsed = JSON.parse(searchParams.filter);
searchParams.filter = JSON.stringify({ and: [parsed, { '==': [{ var: 'project_id' }, filter.projectId] }] }); searchParams.filter = JSON.stringify({ and: [parsed, { '==': [{ var: 'project_id' }, filter.projectId] }] });
} else { } else {
searchParams.filter = JSON.stringify({ and: [{ '==': [{ var: 'project_id' }, filter.projectId] }] }); searchParams.filter = JSON.stringify({ and: [{ '==': [{ var: 'project_id' }, filter.projectId] }] });
}
} }
}
tasksData = await serverProxy.tasks.get(searchParams);
const tasks = tasksData.map((task) => new Task(task)); tasksData = await serverProxy.tasks.get(searchParams);
tasks.count = tasksData.count; const tasks = tasksData.map((task) => new Task(task));
return tasks; tasks.count = tasksData.count;
}; return tasks;
};
cvat.projects.get.implementation = async (filter) => {
checkFilter(filter, { cvat.projects.get.implementation = async (filter) => {
id: isInteger, checkFilter(filter, {
page: isInteger, id: isInteger,
search: isString, page: isInteger,
sort: isString, search: isString,
filter: isString, sort: isString,
}); filter: isString,
});
checkExclusiveFields(filter, ['id'], ['page']);
const searchParams = {}; checkExclusiveFields(filter, ['id'], ['page']);
for (const key of Object.keys(filter)) { const searchParams = {};
if (['id', 'page', 'search', 'sort', 'page', 'filter'].includes(key)) { for (const key of Object.keys(filter)) {
searchParams[key] = filter[key]; if (['id', 'page', 'search', 'sort', 'page', 'filter'].includes(key)) {
} searchParams[key] = filter[key];
} }
}
const projectsData = await serverProxy.projects.get(searchParams);
const projects = projectsData.map((project) => { const projectsData = await serverProxy.projects.get(searchParams);
project.task_ids = project.tasks; const projects = projectsData.map((project) => {
return project; project.task_ids = project.tasks;
}).map((project) => new Project(project)); return project;
}).map((project) => new Project(project));
projects.count = projectsData.count;
projects.count = projectsData.count;
return projects;
}; return projects;
};
cvat.projects.searchNames
.implementation = async (search, limit) => serverProxy.projects.searchNames(search, limit); cvat.projects.searchNames
.implementation = async (search, limit) => serverProxy.projects.searchNames(search, limit);
cvat.cloudStorages.get.implementation = async (filter) => {
checkFilter(filter, { cvat.cloudStorages.get.implementation = async (filter) => {
page: isInteger, checkFilter(filter, {
filter: isString, page: isInteger,
sort: isString, filter: isString,
id: isInteger, sort: isString,
search: isString, id: isInteger,
}); search: isString,
});
checkExclusiveFields(filter, ['id', 'search'], ['page']);
const searchParams = {}; checkExclusiveFields(filter, ['id', 'search'], ['page']);
for (const key of Object.keys(filter)) { const searchParams = {};
if (['page', 'filter', 'sort', 'id', 'search'].includes(key)) { for (const key of Object.keys(filter)) {
searchParams[key] = filter[key]; if (['page', 'filter', 'sort', 'id', 'search'].includes(key)) {
} searchParams[key] = filter[key];
} }
const cloudStoragesData = await serverProxy.cloudStorages.get(searchParams); }
const cloudStorages = cloudStoragesData.map((cloudStorage) => new CloudStorage(cloudStorage)); const cloudStoragesData = await serverProxy.cloudStorages.get(searchParams);
cloudStorages.count = cloudStoragesData.count; const cloudStorages = cloudStoragesData.map((cloudStorage) => new CloudStorage(cloudStorage));
return cloudStorages; cloudStorages.count = cloudStoragesData.count;
}; return cloudStorages;
};
cvat.organizations.get.implementation = async () => {
const organizationsData = await serverProxy.organizations.get(); cvat.organizations.get.implementation = async () => {
const organizations = organizationsData.map((organizationData) => new Organization(organizationData)); const organizationsData = await serverProxy.organizations.get();
return organizations; const organizations = organizationsData.map((organizationData) => new Organization(organizationData));
}; return organizations;
};
cvat.organizations.activate.implementation = (organization) => {
checkObjectType('organization', organization, null, Organization); cvat.organizations.activate.implementation = (organization) => {
config.organizationID = organization.slug; checkObjectType('organization', organization, null, Organization);
}; config.organizationID = organization.slug;
};
cvat.organizations.deactivate.implementation = async () => {
config.organizationID = null; cvat.organizations.deactivate.implementation = async () => {
}; config.organizationID = null;
};
cvat.webhooks.get.implementation = async (filter) => {
checkFilter(filter, { cvat.webhooks.get.implementation = async (filter) => {
page: isInteger, checkFilter(filter, {
id: isInteger, page: isInteger,
projectId: isInteger, id: isInteger,
filter: isString, projectId: isInteger,
search: isString, filter: isString,
sort: isString, search: isString,
}); sort: isString,
});
checkExclusiveFields(filter, ['id', 'projectId'], ['page']);
const searchParams = {}; checkExclusiveFields(filter, ['id', 'projectId'], ['page']);
for (const key of Object.keys(filter)) { const searchParams = {};
if (['page', 'id', 'filter', 'search', 'sort'].includes(key)) { for (const key of Object.keys(filter)) {
searchParams[key] = filter[key]; if (['page', 'id', 'filter', 'search', 'sort'].includes(key)) {
} searchParams[key] = filter[key];
} }
}
if (filter.projectId) { if (filter.projectId) {
if (searchParams.filter) { if (searchParams.filter) {
const parsed = JSON.parse(searchParams.filter); const parsed = JSON.parse(searchParams.filter);
searchParams.filter = JSON.stringify({ and: [parsed, { '==': [{ var: 'project_id' }, filter.projectId] }] }); searchParams.filter = JSON.stringify({ and: [parsed, { '==': [{ var: 'project_id' }, filter.projectId] }] });
} else { } else {
searchParams.filter = JSON.stringify({ and: [{ '==': [{ var: 'project_id' }, filter.projectId] }] }); searchParams.filter = JSON.stringify({ and: [{ '==': [{ var: 'project_id' }, filter.projectId] }] });
}
} }
}
const webhooksData = await serverProxy.webhooks.get(searchParams); const webhooksData = await serverProxy.webhooks.get(searchParams);
const webhooks = webhooksData.map((webhookData) => new Webhook(webhookData)); const webhooks = webhooksData.map((webhookData) => new Webhook(webhookData));
webhooks.count = webhooksData.count; webhooks.count = webhooksData.count;
return webhooks; return webhooks;
}; };
return cvat;
}
module.exports = implementAPI; return cvat;
})(); }

@ -8,34 +8,36 @@
* @module API * @module API
*/ */
function build() { import PluginRegistry from './plugins';
const PluginRegistry = require('./plugins').default; import loggerStorage from './logger-storage';
const loggerStorage = require('./logger-storage').default; import { Log } from './log';
const { Log } = require('./log'); import ObjectState from './object-state';
const ObjectState = require('./object-state').default; import Statistics from './statistics';
const Statistics = require('./statistics').default; import Comment from './comment';
const Comment = require('./comment').default; import Issue from './issue';
const Issue = require('./issue').default; import { Job, Task } from './session';
const { Job, Task } = require('./session'); import Project from './project';
const Project = require('./project').default; import implementProject from './project-implementation';
const implementProject = require('./project-implementation').default; import { Attribute, Label } from './labels';
const { Attribute, Label } = require('./labels'); import MLModel from './ml-model';
const MLModel = require('./ml-model').default; import { FrameData } from './frames';
const { FrameData } = require('./frames'); import CloudStorage from './cloud-storage';
const CloudStorage = require('./cloud-storage').default; import Organization from './organization';
const Organization = require('./organization').default; import Webhook from './webhook';
const Webhook = require('./webhook').default;
import * as enums from './enums';
const enums = require('./enums'); import {
Exception, ArgumentError, DataError, ScriptingError, PluginError, ServerError,
} from './exceptions';
const { import User from './user';
Exception, ArgumentError, DataError, ScriptingError, PluginError, ServerError, import pjson from '../package.json';
} = require('./exceptions'); import config from './config';
const User = require('./user').default; import implementAPI from './api-implementation';
const pjson = require('../package.json');
const config = require('./config').default;
function build() {
/** /**
* API entrypoint * API entrypoint
* @namespace cvat * @namespace cvat
@ -925,9 +927,8 @@ function build() {
cvat.cloudStorages = Object.freeze(cvat.cloudStorages); cvat.cloudStorages = Object.freeze(cvat.cloudStorages);
cvat.organizations = Object.freeze(cvat.organizations); cvat.organizations = Object.freeze(cvat.organizations);
const implementAPI = require('./api-implementation');
const implemented = Object.freeze(implementAPI(cvat)); const implemented = Object.freeze(implementAPI(cvat));
return implemented; return implemented;
} }
module.exports = build(); export default build();

File diff suppressed because it is too large Load Diff

@ -13,7 +13,7 @@ jest.mock('../../src/server-proxy', () => {
}); });
// Initialize api // Initialize api
window.cvat = require('../../src/api'); window.cvat = require('../../src/api').default;
const serverProxy = require('../../src/server-proxy').default; const serverProxy = require('../../src/server-proxy').default;
// Test cases // Test cases

@ -12,7 +12,7 @@ jest.mock('../../src/server-proxy', () => {
}); });
// Initialize api // Initialize api
window.cvat = require('../../src/api'); window.cvat = require('../../src/api').default;
const CloudStorage= require('../../src/cloud-storage').default; const CloudStorage= require('../../src/cloud-storage').default;
const { cloudStoragesDummyData } = require('../mocks/dummy-data.mock'); const { cloudStoragesDummyData } = require('../mocks/dummy-data.mock');

@ -12,7 +12,7 @@ jest.mock('../../src/server-proxy', () => {
}); });
// Initialize api // Initialize api
window.cvat = require('../../src/api'); window.cvat = require('../../src/api').default;
const { FrameData } = require('../../src/frames'); const { FrameData } = require('../../src/frames');

@ -12,7 +12,7 @@ jest.mock('../../src/server-proxy', () => {
}); });
// Initialize api // Initialize api
window.cvat = require('../../src/api'); window.cvat = require('../../src/api').default;
const { Job } = require('../../src/session'); const { Job } = require('../../src/session');

@ -12,7 +12,7 @@ jest.mock('../../src/server-proxy', () => {
}); });
// Initialize api // Initialize api
window.cvat = require('../../src/api'); window.cvat = require('../../src/api').default;
describe('Feature: set attributes for an object state', () => { describe('Feature: set attributes for an object state', () => {
test('set a valid value', () => { test('set a valid value', () => {

@ -12,7 +12,7 @@ jest.mock('../../src/server-proxy', () => {
}); });
// Initialize api // Initialize api
window.cvat = require('../../src/api'); window.cvat = require('../../src/api').default;
describe('Feature: dummy feature', () => { describe('Feature: dummy feature', () => {
test('dummy test', async () => { test('dummy test', async () => {

@ -12,7 +12,7 @@ jest.mock('../../src/server-proxy', () => {
}); });
// Initialize api // Initialize api
window.cvat = require('../../src/api'); window.cvat = require('../../src/api').default;
const Project = require('../../src/project').default; const Project = require('../../src/project').default;

@ -12,7 +12,7 @@ jest.mock('../../src/server-proxy', () => {
}); });
// Initialize api // Initialize api
window.cvat = require('../../src/api'); window.cvat = require('../../src/api').default;
const { AnnotationFormats, Loader, Dumper } = require('../../src/annotation-formats'); const { AnnotationFormats, Loader, Dumper } = require('../../src/annotation-formats');
// Test cases // Test cases

@ -12,7 +12,7 @@ jest.mock('../../src/server-proxy', () => {
}); });
// Initialize api // Initialize api
window.cvat = require('../../src/api'); window.cvat = require('../../src/api').default;
const { Task } = require('../../src/session'); const { Task } = require('../../src/session');

@ -12,7 +12,7 @@ jest.mock('../../src/server-proxy', () => {
}); });
// Initialize api // Initialize api
window.cvat = require('../../src/api'); window.cvat = require('../../src/api').default;
const User = require('../../src/user').default; const User = require('../../src/user').default;

@ -11,7 +11,7 @@ jest.mock('../../src/server-proxy', () => {
}); });
// Initialize api // Initialize api
window.cvat = require('../../src/api'); window.cvat = require('../../src/api').default;
const Webhook = require('../../src/webhook').default; const Webhook = require('../../src/webhook').default;
const { webhooksDummyData, webhooksEventsDummyData } = require('../mocks/dummy-data.mock'); const { webhooksDummyData, webhooksEventsDummyData } = require('../mocks/dummy-data.mock');

@ -8,6 +8,7 @@
"isolatedModules": true, "isolatedModules": true,
"noEmit": true, "noEmit": true,
"baseUrl": "src", "baseUrl": "src",
"resolveJsonModule": true,
}, },
"include": ["src/*.ts"] "include": ["src/*.ts"]
} }

Loading…
Cancel
Save