Skip to content

Commit

Permalink
Merge pull request #14 from digirati-co-uk/feature/update-dependencies
Browse files Browse the repository at this point in the history
Updated various dependencies
  • Loading branch information
stephenwf authored Oct 19, 2021
2 parents 1aac024 + 3bcce27 commit 8426777
Show file tree
Hide file tree
Showing 37 changed files with 1,410 additions and 1,163 deletions.
34 changes: 17 additions & 17 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
module.exports = {
extends: [
'@fesk/standard',
'plugin:import/typescript',
],
rules: {
'react/prop-types': 0,
'import/named': 0,
'@typescript-eslint/explicit-function-return-type': 0,
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/ban-ts-ignore': 1,
'@typescript-eslint/no-var-requires': 0,
'@typescript-eslint/camelcase': 0,
},
settings: {
"react": {
"version": "detect",
},
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'prettier'],
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
rules: {
'prettier/prettier': 'error',
'react/prop-types': 0,
'import/named': 0,
'no-undef': 0,
'@typescript-eslint/explicit-function-return-type': 0,
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/no-var-requires': 0,
'@typescript-eslint/camelcase': 0,
},
settings: {
react: {
version: 'detect',
},
},
};
8 changes: 5 additions & 3 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
singleQuote: true
trailingComma: es5
printWidth: 120
{
"singleQuote": true,
"trailingComma": "es5",
"printWidth": 120
}
45 changes: 40 additions & 5 deletions __tests__/example.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// For coverage.
import { createApp } from '../src/app';
import { Worker } from 'bullmq';

beforeAll(async () => {
await global.setApp(baseConfig => createApp(baseConfig));
await global.setApp((baseConfig) => createApp(baseConfig));
});

describe('Creating and fetching a task', () => {
Expand All @@ -23,17 +24,51 @@ describe('Creating and fetching a task', () => {
expect(created.status).toEqual(201);

// Make sure the body matches
expect(created.body.type).toEqual('task-type-a');
expect(created.body.name).toEqual('A test task');
expect((created.body as any).type).toEqual('task-type-a');
expect((created.body as any).name).toEqual('A test task');

// Fetching task.
const fetched = await global.asAdmin.get(`/tasks/${created.body.id}`);
const fetched = await global.asAdmin.get(`/tasks/${(created.body as any).id}`);
expect(fetched.status).toEqual(200);
});

test('We should now have one task', async () => {
const tasks = await global.asAdmin.get('/tasks');

expect(tasks.body.tasks).toHaveLength(1);
expect((tasks.body as any).tasks).toHaveLength(1);
});

test('Simple event', async () => {
expect.assertions(4);

const worker = new Promise<Worker>((resolve) => {
const _worker = new Worker(
'jest',
async (job) => {
expect(job.name).toEqual('created');
expect(job.data.type).toEqual('task-type-a');
expect(job.data.context[0]).toEqual('urn:madoc:site:456');
resolve(_worker);
},
global.workerOptions
);
});

const created = await global.asAdmin.post('/tasks', {
type: 'task-type-a',
name: 'A test task',
description: `A description of a test task`,
subject: 'my-subject',
state: {},
events: ['jest.created'],
status: 0,
status_text: 'not started',
parameters: ['param-1', 'param-2'],
});

expect(created.status).toEqual(201);

await (await worker).disconnect();
await (await worker).close(true);
});
});
9 changes: 9 additions & 0 deletions jest-testcontainers-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ module.exports = {
text: 'server started',
},
},
redis: {
image: 'redis',
tag: '5-alpine',
ports: [6379],
wait: {
type: 'text',
text: 'Ready to accept connections',
},
},
};
35 changes: 27 additions & 8 deletions jest.environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import MockReq from 'mock-req';
// @ts-ignore
import MockRes from 'mock-res';
import compose from 'koa-compose';
import { AppConfig } from './src/types';
import { WorkerOptions } from 'bullmq';

type NewIncomingMessage = IncomingMessage & Transform;

Expand Down Expand Up @@ -42,8 +44,11 @@ declare global {
asUser: (user: Partial<{ name: string; scope: string; iss: string; sub: string }>) => MiniApi;
__TESTCONTAINERS__: Array<any>;
__TESTCONTAINERS_POSTGRES_IP__: string;
__TESTCONTAINERS_REDIS_IP__: string;
__TESTCONTAINERS_POSTGRES_NAME__: string;
__TESTCONTAINERS_POSTGRES_PORT_5432__: number;
__TESTCONTAINERS_REDIS_PORT_6379__: number;
workerOptions: WorkerOptions;
}
}
}
Expand All @@ -58,11 +63,13 @@ class TaskAPIEnvironment extends TestcontainersEnvironment {
async setup() {
await super.setup();

const host = this.global.__TESTCONTAINERS_POSTGRES_IP__;
const port = this.global.__TESTCONTAINERS_POSTGRES_PORT_5432__;
const pgHost = this.global.__TESTCONTAINERS_POSTGRES_IP__;
const pgPort = this.global.__TESTCONTAINERS_POSTGRES_PORT_5432__;
const redisHost = this.global.__TESTCONTAINERS_REDIS_IP__;
const redisPort = this.global.__TESTCONTAINERS_REDIS_PORT_6379__;

// @ts-ignore
this.postgresUri = `postgresql://postgres:postgres@${host}:${port}/postgres`;
this.postgresUri = `postgresql://postgres:postgres@${pgHost}:${pgPort}/postgres`;
const slonik = createPool(this.postgresUri);

this.migrator = setupSlonikMigrator({
Expand All @@ -75,14 +82,26 @@ class TaskAPIEnvironment extends TestcontainersEnvironment {
});

this.migrations = await this.migrator.up();

this.global.setApp = async cb => {
this.global.workerOptions = {
connection: {
host: redisHost,
port: redisPort,
db: 2,
},
};
this.global.setApp = async (cb: (options: AppConfig) => Koa | Promise<Koa>) => {
await this.setApp(
await cb({
postgres: this.postgresUri,
env: 'test',
queueList: [],
queueList: ['jest'],
migrate: false,
enableQueue: true,
redis: {
host: redisHost,
port: redisPort,
db: 2,
},
})
);
};
Expand Down Expand Up @@ -123,7 +142,7 @@ class TaskAPIEnvironment extends TestcontainersEnvironment {
await (app as any).handleRequest(ctx, fn);

if (ctx.response.body && ctx.response.is('application/json')) {
ctx.response.body = JSON.parse(ctx.response.body);
ctx.response.body = JSON.parse(ctx.response.body as any);
}

return ctx.response;
Expand Down Expand Up @@ -213,7 +232,7 @@ class TaskAPIEnvironment extends TestcontainersEnvironment {
}

async stopApp() {
await new Promise(resolve => {
await new Promise<void>(resolve => {
if (this.serverListener) {
this.serverListener.close(() => {
resolve();
Expand Down
43 changes: 21 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,45 @@
"dependencies": {
"pg": "^8.7.1",
"pm2": "^5.1.2",
"bullmq": "^1.8.5"
"bullmq": "^1.50.3"
},
"devDependencies": {
"@koa/router": "^8.0.6",
"@koa/router": "^10.1.1",
"@slonik/migrator": "^0.2.0",
"@slonik/typegen": "^0.3.1",
"ajv": "^6.12.0",
"bullmq": "^1.8.5",
"koa": "^2.11.0",
"koa-body": "^4.1.1",
"ajv": "^8.6.3",
"bullmq": "^1.50.3",
"koa": "^2.13.4",
"koa-body": "^4.2.0",
"koa-json": "^2.0.2",
"koa-logger": "^3.2.1",
"slonik": "^22.4.3",
"slonik": "^24.1.2",
"uuid": "^8.3.2",
"@fesk/eslint-config-standard": "1.4.0-pr.b6dc2fa3",
"@trendyol/jest-testcontainers": "^2.1.0",
"@types/jest": "^27.0.2",
"@types/koa": "^2.11.0",
"@types/koa": "^2.13.4",
"@types/koa-json": "^2.0.20",
"@types/koa-logger": "^3.1.1",
"@types/koa__router": "^8.0.2",
"@types/koa-logger": "^3.1.2",
"@types/koa__router": "^8.0.8",
"@types/slonik": "^22.1.5",
"@types/uuid": "^7.0.0",
"@typescript-eslint/eslint-plugin": "^2.4.0",
"@typescript-eslint/parser": "^2.4.0",
"@types/uuid": "^8.3.1",
"@typescript-eslint/eslint-plugin": "^5.1.0",
"@typescript-eslint/parser": "^5.1.0",
"esbuild": "^0.13.8",
"eslint": "^6.8.0",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-prettier": "^3.1.1",
"eslint": "^8.0.1",
"eslint-plugin-babel": "^5.3.1",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react-hooks": "^4.2.0",
"jest": "^27.2.5",
"jest": "^27.3.1",
"koa-create-context": "^1.0.2",
"mock-req": "^0.2.0",
"mock-res": "^0.5.0",
"nodemon": "^2.0.2",
"prettier": "^1.19.1",
"prettier": "^2.4.1",
"ts-jest": "^27.0.7",
"ts-node": "^8.6.2",
"typescript": "^3.8.3",
"typescript-json-schema": "^0.42.0"
"ts-node": "^10.3.0",
"typescript": "^4.4.4",
"typescript-json-schema": "^0.51.0"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 3 additions & 0 deletions schemas/create-task.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
},
"type": "array"
},
"metadata": {
"description": "Custom metadata."
},
"name": {
"description": "A human-readable name for your task.",
"type": "string"
Expand Down
3 changes: 3 additions & 0 deletions schemas/update-task.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"description": "An optional description of the task.",
"type": "string"
},
"metadata": {
"description": "Any metadata on the task."
},
"name": {
"description": "The name of the task.",
"type": "string"
Expand Down
4 changes: 2 additions & 2 deletions src/database/get-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export async function getTask(
`
);

const actualTask = taskList.find(t => t.id === id);
const subtasks = taskList.filter(t => t.id !== id);
const actualTask = taskList.find((t) => t.id === id);
const subtasks = taskList.filter((t) => t.id !== id);

if (!actualTask) {
throw new NotFound();
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function main() {
}
}

main().catch(err => {
main().catch((err) => {
console.error(err);
process.exit(1);
});
14 changes: 8 additions & 6 deletions src/middleware/db-connection.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Middleware } from 'koa';
import { DatabasePoolType } from 'slonik';

export const dbConnection = (pool: DatabasePoolType): Middleware => async (context, next) => {
await pool.connect(async connection => {
context.connection = connection;
await next();
});
};
export const dbConnection =
(pool: DatabasePoolType): Middleware =>
async (context, next) => {
await pool.connect(async (connection) => {
context.connection = connection;
await next();
});
};
2 changes: 1 addition & 1 deletion src/middleware/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const errorHandler: Middleware = async (context, next) => {
await next();
} catch (err) {
if (process.env.NODE_ENV === 'test') {
console.log(err.toString());
console.log((err as Error).toString());
}

if (err instanceof RequestError) {
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/parse-jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function parseJwt(_config: JWTConfig = {}): RouteMiddleware {
}
: undefined;

const jwt = parseToken(token, config, asUser);
const jwt = parseToken(token, config, asUser as any);

if (!jwt) {
throw new NotFound();
Expand Down
Loading

0 comments on commit 8426777

Please sign in to comment.