Browse Source

feat: apiTokens apis

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5444/head
Pranav C 2 years ago
parent
commit
1bc1868e9d
  1. 4
      packages/nocodb-nest/src/app.module.ts
  2. 20
      packages/nocodb-nest/src/modules/api-tokens/api-tokens.controller.spec.ts
  3. 49
      packages/nocodb-nest/src/modules/api-tokens/api-tokens.controller.ts
  4. 9
      packages/nocodb-nest/src/modules/api-tokens/api-tokens.module.ts
  5. 18
      packages/nocodb-nest/src/modules/api-tokens/api-tokens.service.spec.ts
  6. 43
      packages/nocodb-nest/src/modules/api-tokens/api-tokens.service.ts
  7. 20
      packages/nocodb-nest/src/modules/hook-filters/hook-filters.controller.spec.ts
  8. 8
      packages/nocodb-nest/src/modules/hook-filters/hook-filters.controller.ts
  9. 9
      packages/nocodb-nest/src/modules/hook-filters/hook-filters.module.ts
  10. 18
      packages/nocodb-nest/src/modules/hook-filters/hook-filters.service.spec.ts
  11. 4
      packages/nocodb-nest/src/modules/hook-filters/hook-filters.service.ts

4
packages/nocodb-nest/src/app.module.ts

@ -27,9 +27,11 @@ import { GridColumnsModule } from './modules/grid-columns/grid-columns.module';
import { MapsModule } from './modules/maps/maps.module'; import { MapsModule } from './modules/maps/maps.module';
import { ProjectUsersModule } from './modules/project-users/project-users.module'; import { ProjectUsersModule } from './modules/project-users/project-users.module';
import { ModelVisibilitiesModule } from './modules/model-visibilities/model-visibilities.module'; import { ModelVisibilitiesModule } from './modules/model-visibilities/model-visibilities.module';
import { HookFiltersModule } from './modules/hook-filters/hook-filters.module';
import { ApiTokensModule } from './modules/api-tokens/api-tokens.module';
@Module({ @Module({
imports: [AuthModule, UsersModule, UtilsModule, ProjectsModule, TablesModule, ViewsModule, FiltersModule, SortsModule, ColumnsModule, ViewColumnsModule, BasesModule, HooksModule, SharedBasesModule, FormsModule, GridsModule, KanbansModule, GalleriesModule, FormColumnsModule, GridColumnsModule, MapsModule, ProjectUsersModule, ModelVisibilitiesModule], imports: [AuthModule, UsersModule, UtilsModule, ProjectsModule, TablesModule, ViewsModule, FiltersModule, SortsModule, ColumnsModule, ViewColumnsModule, BasesModule, HooksModule, SharedBasesModule, FormsModule, GridsModule, KanbansModule, GalleriesModule, FormColumnsModule, GridColumnsModule, MapsModule, ProjectUsersModule, ModelVisibilitiesModule, HookFiltersModule, ApiTokensModule],
controllers: [], controllers: [],
providers: [Connection, MetaService, JwtStrategy, ExtractProjectIdMiddleware], providers: [Connection, MetaService, JwtStrategy, ExtractProjectIdMiddleware],
exports: [Connection, MetaService], exports: [Connection, MetaService],

20
packages/nocodb-nest/src/modules/api-tokens/api-tokens.controller.spec.ts

@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ApiTokensController } from './api-tokens.controller';
import { ApiTokensService } from './api-tokens.service';
describe('ApiTokensController', () => {
let controller: ApiTokensController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ApiTokensController],
providers: [ApiTokensService],
}).compile();
controller = module.get<ApiTokensController>(ApiTokensController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

49
packages/nocodb-nest/src/modules/api-tokens/api-tokens.controller.ts

@ -0,0 +1,49 @@
import {
Controller,
Delete,
Get,
Post,
UseGuards,
Request,
Body,
Param,
} from '@nestjs/common';
import { PagedResponseImpl } from '../../helpers/PagedResponse';
import {
Acl,
ExtractProjectIdMiddleware,
} from '../../middlewares/extract-project-id/extract-project-id.middleware';
import { ApiTokensService } from './api-tokens.service';
import { AuthGuard } from '@nestjs/passport';
@Controller()
@UseGuards(ExtractProjectIdMiddleware, AuthGuard('jwt'))
export class ApiTokensController {
constructor(private readonly apiTokensService: ApiTokensService) {}
@Get('/api/v1/db/meta/projects/:projectId/api-tokens')
@Acl('apiTokenList')
async apiTokenList(@Request() req) {
return new PagedResponseImpl(
await this.apiTokensService.apiTokenList({ userId: req['user'].id }),
);
}
@Post('/api/v1/db/meta/projects/:projectId/api-tokens')
@Acl('apiTokenCreate')
async apiTokenCreate(@Request() req, @Body() body) {
return await this.apiTokensService.apiTokenCreate({
tokenBody: body,
userId: req['user'].id,
});
}
@Delete('/api/v1/db/meta/projects/:projectId/api-tokens/:token')
@Acl('apiTokenDelete')
async apiTokenDelete(@Request() req, @Param('token') token: string) {
return await this.apiTokensService.apiTokenDelete({
token,
user: req['user'],
});
}
}

9
packages/nocodb-nest/src/modules/api-tokens/api-tokens.module.ts

@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { ApiTokensService } from './api-tokens.service';
import { ApiTokensController } from './api-tokens.controller';
@Module({
controllers: [ApiTokensController],
providers: [ApiTokensService]
})
export class ApiTokensModule {}

18
packages/nocodb-nest/src/modules/api-tokens/api-tokens.service.spec.ts

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ApiTokensService } from './api-tokens.service';
describe('ApiTokensService', () => {
let service: ApiTokensService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ApiTokensService],
}).compile();
service = module.get<ApiTokensService>(ApiTokensService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

43
packages/nocodb-nest/src/modules/api-tokens/api-tokens.service.ts

@ -0,0 +1,43 @@
import { Injectable } from '@nestjs/common';
import { T } from 'nc-help';
import { ApiTokenReqType, OrgUserRoles } from 'nocodb-sdk'
import { validatePayload } from '../../helpers'
import { NcError } from '../../helpers/catchError'
import { ApiToken, User } from '../../models'
@Injectable()
export class ApiTokensService {
async apiTokenList(param: { userId: string }) {
return await ApiToken.list(param.userId);
}
async apiTokenCreate(param: {
userId: string;
tokenBody: ApiTokenReqType;
}) {
validatePayload(
'swagger.json#/components/schemas/ApiTokenReq',
param.tokenBody
);
T.emit('evt', { evt_type: 'apiToken:created' });
return await ApiToken.insert({
...param.tokenBody,
fk_user_id: param.userId,
});
}
async apiTokenDelete(param: { token; user: User }) {
const apiToken = await ApiToken.getByToken(param.token);
if (
!param.user.roles.includes(OrgUserRoles.SUPER_ADMIN) &&
apiToken.fk_user_id !== param.user.id
) {
NcError.notFound('Token not found');
}
T.emit('evt', { evt_type: 'apiToken:deleted' });
// todo: verify token belongs to the user
return await ApiToken.delete(param.token);
}
}

20
packages/nocodb-nest/src/modules/hook-filters/hook-filters.controller.spec.ts

@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HookFiltersController } from './hook-filters.controller';
import { HookFiltersService } from './hook-filters.service';
describe('HookFiltersController', () => {
let controller: HookFiltersController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [HookFiltersController],
providers: [HookFiltersService],
}).compile();
controller = module.get<HookFiltersController>(HookFiltersController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

8
packages/nocodb-nest/src/modules/hook-filters/hook-filters.controller.ts

@ -0,0 +1,8 @@
import { Controller } from '@nestjs/common';
import { HookFiltersService } from './hook-filters.service';
@Controller('hook-filters')
export class HookFiltersController {
constructor(private readonly hookFiltersService: HookFiltersService) {}
}

9
packages/nocodb-nest/src/modules/hook-filters/hook-filters.module.ts

@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { HookFiltersService } from './hook-filters.service';
import { HookFiltersController } from './hook-filters.controller';
@Module({
controllers: [HookFiltersController],
providers: [HookFiltersService]
})
export class HookFiltersModule {}

18
packages/nocodb-nest/src/modules/hook-filters/hook-filters.service.spec.ts

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HookFiltersService } from './hook-filters.service';
describe('HookFiltersService', () => {
let service: HookFiltersService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [HookFiltersService],
}).compile();
service = module.get<HookFiltersService>(HookFiltersService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

4
packages/nocodb-nest/src/modules/hook-filters/hook-filters.service.ts

@ -0,0 +1,4 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class HookFiltersService {}
Loading…
Cancel
Save