Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- run: yarn install
- run: yarn format
- run: yarn lint
- run: yarn build
- run: yarn test:bin
- run: yarn test --coverage
- run: yarn check-git
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
**/node_modules
**/*.log
coverage
dist

# Other
**/.idea
Expand Down
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ coverage
*.iml
**/__tests__
**/__mocks__
.github
.yarn
.yarnrc.yml
src
test
.tsconfig
22 changes: 18 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@
"author": "DevSide",
"license": "MIT",
"type": "module",
"bin": "bin/dynamock.js",
"main": "src/createServer.js",
"bin": "./dist/bin/dynamock.js",
"main": "dist/createServer.js",
"scripts": {
"build": "rm -rf dist/ && tsc && chmod +x ./dist/bin/dynamock.js && yarn link",
"check-git": "git status -uno --porcelain && [ -z \"$(git status -uno --porcelain)\" ] || (echo 'Git working directory not clean'; false)",
"format": "npx @biomejs/biome format --write src",
"lint": "npx @biomejs/biome lint --write src",
"release": "semantic-release",
"ts": "tsc --noEmit",
"test": "NODE_OPTIONS='--enable-source-maps --experimental-vm-modules' jest src --coverage",
"test:bin": "node test/bin.js"
},
"engines": {
"node": ">=20"
},
"jest": {
"transform": {}
"preset": "ts-jest",
"transform": {
"^.+\\.ts$": "ts-jest"
},
"moduleNameMapper": {
"^(\\.\\.?\\/.+)\\.js$": "$1"
}
},
"dependencies": {
"@hapi/joi": "17.1.1",
Expand All @@ -43,9 +51,15 @@
"@semantic-release/github": "^11.0.0",
"@semantic-release/npm": "^12.0.1",
"@semantic-release/release-notes-generator": "^14.0.1",
"@types/cookie-parser": "^1.4.7",
"@types/express": "^5.0.0",
"@types/hapi__joi": "^17.1.14",
"@types/supertest": "^6.0.2",
"jest": "^29.7.0",
"semantic-release": "^24.1.2",
"supertest": "^7.0.0"
"supertest": "^7.0.0",
"ts-jest": "^29.2.5",
"typescript": "^5.6.3"
},
"packageManager": "yarn@4.5.0"
}
16 changes: 10 additions & 6 deletions src/__tests__/integrations.js → src/__tests__/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { beforeAll, afterEach, afterAll, describe, test } from '@jest/globals'
import { dirname } from 'node:path'
import { mkdirSync, writeFileSync } from 'node:fs'
import supertest from 'supertest'
import { createServer } from '../createServer'
import { createServer } from '../createServer.js'

type Method = 'options' | 'put' | 'get' | 'post' | 'head' | 'delete' | 'patch'

describe('integrations.js', () => {
let server
let request
let server = createServer()
let request = supertest.agent(server)

beforeAll((done) => {
server = createServer()
Expand All @@ -18,7 +20,6 @@ describe('integrations.js', () => {

afterAll((done) => {
server.close(done)
server = null
})

describe('manipulate configuration', () => {
Expand Down Expand Up @@ -444,7 +445,7 @@ describe('integrations.js', () => {
})
.expect(201)

await request[method](path)
await request[method as Method](path)
.set({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
Expand Down Expand Up @@ -539,7 +540,7 @@ describe('integrations.js', () => {
})
.expect(201)

await request[method](path).expect(shouldMatch ? 200 : 404)
await request[method as Method](path).expect(shouldMatch ? 200 : 404)
},
)
})
Expand Down Expand Up @@ -680,6 +681,7 @@ describe('integrations.js', () => {

const cookies = Object.entries(values)
.reduce((acc, [key, value]) => {
// @ts-ignore
acc.push(`${key}=${value}`)
return acc
}, [])
Expand Down Expand Up @@ -1006,12 +1008,14 @@ describe('integrations.js', () => {

if (property === 'headers') {
for (const key in expectedPropertyValue) {
// @ts-ignore
r.expect(key, expectedPropertyValue[key])
}
} else {
const cookieValue = []

for (const key in expectedPropertyValue) {
// @ts-ignore
cookieValue.push(`${key}=${expectedPropertyValue[key]}; Path=/`)
}

Expand Down
6 changes: 3 additions & 3 deletions bin/dynamock.js → src/bin/dynamock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import { createServer } from "../src/createServer.js"
import { createServer } from '../createServer.js'

const [, , port, host = '127.0.0.1'] = process.argv

Expand All @@ -10,11 +10,11 @@ if (!port) {

const server = createServer()

server.listen(port, host, () => {
server.listen(Number(port), host, () => {
console.log(`dynamock is running on port ${port}...`)
})

function shutDown () {
function shutDown() {
console.log('dynamock is shutting down gracefully')
server.close(() => {
process.exit(0)
Expand Down
25 changes: 22 additions & 3 deletions src/configuration.js → src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import Joi from '@hapi/joi'

export type ConfigurationObjectType = {
[key: string]: {
[key: string]: string
}
}

export type ConfigurationType = {
cors: null | '*'
headers: ConfigurationObjectType
query: ConfigurationObjectType
cookies: ConfigurationObjectType
}

const schema = Joi.object({
cors: Joi.alternatives([Joi.string().valid('*'), Joi.object().valid(null)]),
headers: Joi.object(),
query: Joi.object(),
cookies: Joi.object(),
}).required()

export function validateConfiguration(unsafeConfiguration) {
export function validateConfiguration(unsafeConfiguration: unknown) {
return schema.validate(unsafeConfiguration).error
}

export function createConfiguration() {
export function createConfiguration(): ConfigurationType {
return {
cors: null,
headers: {},
Expand All @@ -20,7 +33,13 @@ export function createConfiguration() {
}
}

export function updateConfiguration(configuration, cors, headers, query, cookies) {
export function updateConfiguration(
configuration: ConfigurationType,
cors: undefined | null | '*',
headers: undefined | ConfigurationObjectType,
query: undefined | ConfigurationObjectType,
cookies: undefined | ConfigurationObjectType,
) {
if (cors !== undefined) {
configuration.cors = cors === '*' ? '*' : null
}
Expand Down
185 changes: 0 additions & 185 deletions src/createServer.js

This file was deleted.

Loading
Loading