Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(deps): bump axios from 1.4.0 to 1.6.1 in /ui #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -8,7 +8,6 @@
"start:dev": "npx nodemon"
},
"devDependencies": {
"@koa/router": "^12.0.0",
"@types/js-yaml": "^4.0.5",
"@types/koa": "^2.13.8",
"@types/koa__router": "^12.0.0",
Expand All @@ -32,6 +31,7 @@
"prettier": "^2.8.8"
},
"dependencies": {
"@koa/router": "^12.0.0",
"dotenv": "^10.0.0",
"http-status-codes": "^2.2.0",
"js-yaml": "^4.1.0",
Expand All @@ -56,4 +56,4 @@
},
"author": "",
"license": "ISC"
}
}
2 changes: 1 addition & 1 deletion src/helpers/common.ts
Expand Up @@ -3,7 +3,7 @@ export const Headers = {
};

export const ErrorMessages = {
SWAGGER_NOT_PARSABLE: 'Could not parse swagger data',
SWAGGER_NOT_PARSABLE: 'Could not parse swagger file content',
SWAGGER_NAME_ALREADY_EXISTS: 'Swagger with the same name already exists',
SWAGGER_TITLE_ALREADY_EXISTS: 'Swagger with the same title already exists',
SWAGGER_NOT_FOUND: 'Swagger not found',
Expand Down
1 change: 1 addition & 0 deletions src/server/database/connectors/consts.ts
@@ -1,3 +1,4 @@
export const TableNames = {
SWAGGERS_TABLE_NAME: 'swaggers',
SWAGGERS_CONTENT_TABLE_NAME: 'swaggers_content',
};
21 changes: 18 additions & 3 deletions src/server/database/connectors/sequlize/migrations/00_initial.ts
@@ -1,7 +1,17 @@
import { DataTypes } from 'sequelize';
import { TableNames } from '../../consts';

export const up = async ({ context }) =>
export const up = async ({ context }) => {
await context.createTable(TableNames.SWAGGERS_CONTENT_TABLE_NAME, {
id: {
type: DataTypes.UUID,
primaryKey: true,
},
file_content: {
type: DataTypes.TEXT('long'),
allowNull: false,
},
});
await context.createTable(TableNames.SWAGGERS_TABLE_NAME, {
id: {
type: DataTypes.UUID,
Expand All @@ -12,9 +22,13 @@ export const up = async ({ context }) =>
type: DataTypes.STRING,
allowNull: false,
},
data: {
type: DataTypes.TEXT('long'),
file_content_id: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: TableNames.SWAGGERS_CONTENT_TABLE_NAME,
key: 'id',
},
},
created_at: {
type: DataTypes.DATE,
Expand All @@ -25,6 +39,7 @@ export const up = async ({ context }) =>
allowNull: false,
},
});
};

export const down = async ({ context: queryInterface }) =>
await queryInterface.dropTable(TableNames.SWAGGERS_TABLE_NAME);
104 changes: 88 additions & 16 deletions src/server/database/models/swagger.ts
@@ -1,36 +1,67 @@
import { v4 } from 'uuid';
import { DataTypes, Model, Optional, Sequelize } from 'sequelize';
import { DataTypes, Model, ModelStatic, Sequelize } from 'sequelize';
import { TableNames } from '../connectors/consts';

export enum SwaggerFormats {
Yaml = 'yaml',
Json = 'json',
}

interface SwaggerAttributes {
export interface SwaggerFileContent {
id: string;
file_content: string;
}

export interface InternalSwaggerResource {
id: string;
name: string;
data: string;
file_content_id?: string;
FileContent?: SwaggerFileContent;
created_at?: Date;
updated_at?: Date;
}
export type SwaggerRequestBody = Optional<
SwaggerAttributes,
'id' | 'created_at' | 'updated_at'
>;
export type SwaggerResource = Required<SwaggerAttributes>;
export type SwaggerRequestBody = {
name: string;
file_content: string;
};
export type SwaggerResource = Omit<
InternalSwaggerResource,
'file_content_id'
> & {
file_content?: string;
};

class SwaggerModel
extends Model<SwaggerAttributes, SwaggerRequestBody>
implements SwaggerAttributes
extends Model<InternalSwaggerResource, SwaggerRequestBody>
implements InternalSwaggerResource
{
public static FileContent: ModelStatic<Model<SwaggerFileContent>>;

public id!: string;
public name!: string;
public data!: string;
public readonly file_content_id!: string;
public readonly created_at!: Date;
public readonly updated_at!: Date;

public static initializeModel(sequelizeClient: Sequelize): void {
this.FileContent = sequelizeClient.define(
'FileContent',
{
id: {
type: DataTypes.UUID,
primaryKey: true,
},
file_content: {
type: DataTypes.TEXT('long'),
allowNull: false,
},
},
{
tableName: TableNames.SWAGGERS_CONTENT_TABLE_NAME,
timestamps: false,
}
);

this.init(
{
id: {
Expand All @@ -41,27 +72,68 @@ class SwaggerModel
type: DataTypes.STRING,
allowNull: false,
},
data: {
type: DataTypes.TEXT('long'),
allowNull: false,
},
},
{
tableName: TableNames.SWAGGERS_TABLE_NAME,
sequelize: sequelizeClient,
}
);

this.belongsTo(this.FileContent, {
foreignKey: {
name: 'file_content_id',
allowNull: false,
},
});
}

public static fromRequest(requestBody: SwaggerRequestBody): SwaggerResource {
const now = new Date();

return {
id: v4(),
...requestBody,
name: requestBody.name,
file_content: requestBody.file_content,
created_at: now,
updated_at: now,
};
}

public static toResponse(swagger: SwaggerModel): SwaggerResource {
const { file_content_id, FileContent, ...restSwaggerProps } =
swagger.dataValues;

if (FileContent) {
return this.toResponseWithFileContent(restSwaggerProps, FileContent);
}

return restSwaggerProps;
}

private static toResponseWithFileContent(
swagger: SwaggerResource,
swaggerFileContent: SwaggerFileContent
): SwaggerResource {
return {
id: swagger.id,
name: swagger.name,
file_content: swaggerFileContent.file_content,
created_at: swagger.created_at,
updated_at: swagger.updated_at,
};
}

public static async includeFileContent(
swagger: SwaggerModel
): Promise<SwaggerResource> {
const fileContent = (await swagger[
'getFileContent'
]()) as SwaggerFileContent;

const { file_content_id, ...restSwaggerProps } = swagger.dataValues;

return this.toResponseWithFileContent(restSwaggerProps, fileContent);
}
}

export default SwaggerModel;
57 changes: 44 additions & 13 deletions src/server/database/repositories/swaggerRepo.ts
@@ -1,41 +1,72 @@
import { v4 } from 'uuid';
import SwaggerModel, {
SwaggerRequestBody,
InternalSwaggerResource,
SwaggerResource,
} from '../models/swagger';

export class SwaggersRepository {
public static async getAllSwaggers(): Promise<SwaggerResource[]> {
const swaggers = await SwaggerModel.findAll();
public static async getAllSwaggers(
includeFileContent = false
): Promise<SwaggerModel[]> {
const swaggers = await SwaggerModel.findAll({
include: includeFileContent ? SwaggerModel.FileContent : undefined,
});

return swaggers;
}
public static async getSwagger(id: string): Promise<SwaggerResource> {
public static async getSwagger(id: string): Promise<SwaggerModel> {
const swaggers = await SwaggerModel.findByPk(id);
return swaggers;
}

public static async createSwagger(
swagger: SwaggerResource
): Promise<SwaggerResource> {
const createdSwagger = await SwaggerModel.create(swagger);
return createdSwagger;
const { file_content, ...restSwaggerProps } = swagger;
const swaggerFileContent = {
id: v4(),
file_content: file_content,
};

await SwaggerModel.FileContent.create(swaggerFileContent);
await SwaggerModel.create({
...restSwaggerProps,
file_content_id: swaggerFileContent.id,
} as InternalSwaggerResource);

return swagger;
}

public static async updateSwagger(
existingResource: SwaggerModel,
payload: SwaggerRequestBody
): Promise<SwaggerResource> {
const updatedSwagger = await existingResource.update({
...payload,
updated_at: new Date(),
});
): Promise<void> {
if (payload.name) {
await existingResource.update({
name: payload.name,
updated_at: new Date(),
});
}

return updatedSwagger;
if (payload.file_content) {
await SwaggerModel.FileContent.update(
{
file_content: payload.file_content,
},
{ where: { id: existingResource.file_content_id } }
);
}
}

public static async deleteSwagger(id: string): Promise<boolean> {
public static async deleteSwagger(swagger: SwaggerModel): Promise<boolean> {
const deletedSwagger = await SwaggerModel.destroy({
where: { id },
where: { id: swagger.id },
});
SwaggerModel.FileContent.destroy({
where: { id: swagger.file_content_id },
});

return !!deletedSwagger;
}
}
4 changes: 2 additions & 2 deletions src/server/middlewares/findSwagger.ts
@@ -1,12 +1,12 @@
import { Context, Next } from 'koa';
import { StatusCodes } from 'http-status-codes';
import { SwaggerResource } from '../database/models/swagger';
import SwaggerModel from '../database/models/swagger';
import { SwaggersRepository } from '../database/repositories/swaggerRepo';
import { ErrorMessages } from '../../helpers/common';

declare module 'koa' {
interface ExtendableContext {
swaggerResource?: SwaggerResource;
swaggerResource?: SwaggerModel;
}
}

Expand Down