Skip to content

Commit

Permalink
Move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
greguz committed Oct 31, 2023
1 parent 84bf391 commit 7b347db
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .c8rc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"all": true,
"reporter": ["html", "lcov", "text"],
"include": [
"fnap.js"
"fnap.mjs"
],
"exclude": [
"test"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
*.log
*.tgz
coverage
fnap.cjs
node_modules
package-lock.json
2 changes: 1 addition & 1 deletion fnap.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FastifyPluginCallback } from 'fastify'
import type { FastifyPluginCallback } from 'fastify'

export interface FastifyNoAdditionalPropertiesOptions {
/**
Expand Down
10 changes: 4 additions & 6 deletions fnap.js → fnap.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict'

const plugin = require('fastify-plugin')
import plugin from 'fastify-plugin'

function mapValues (object, iteratee) {
const result = {}
Object.keys(object).forEach(key => {
for (const key of Object.keys(object)) {
result[key] = iteratee(object[key], key, object)
})
}
return result
}

Expand Down Expand Up @@ -64,7 +62,7 @@ function fnap (fastify, options, callback) {
callback()
}

module.exports = plugin(fnap, {
export default plugin(fnap, {
fastify: '>=3.0.0',
name: 'fastify-no-additional-properties'
})
20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
"name": "fastify-no-additional-properties",
"version": "2.4.0",
"description": "Add additionalProperties: false by default to your JSON Schemas",
"main": "./fnap.js",
"type": "module",
"exports": {
"import": "./fnap.mjs",
"require": "./fnap.cjs",
"types": "./fnap.d.ts"
},
"main": "./fnap.cjs",
"types": "./fnap.d.ts",
"scripts": {
"build": "rollup -c",
"lint": "standard",
"test": "c8 ava",
"prepublishOnly": "npm run lint && npm run test"
"prepublishOnly": "npm run lint && npm run build && npm test"
},
"keywords": [
"fastify",
Expand All @@ -22,7 +29,8 @@
"url": "https://github.com/greguz/fastify-no-additional-properties.git"
},
"files": [
"fnap.js",
"fnap.cjs",
"fnap.mjs",
"fnap.d.ts"
],
"dependencies": {
Expand All @@ -33,6 +41,12 @@
"c8": "^8.0.1",
"fastify": "^4.24.3",
"fluent-json-schema": "^4.1.2",
"rollup": "^4.2.0",
"standard": "^17.1.0"
},
"ava": {
"environmentVariables": {
"NODE_ENV": "test"
}
}
}
8 changes: 8 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
input: './fnap.mjs',
output: {
file: './fnap.cjs',
format: 'cjs'
},
external: ['fastify-plugin']
}
8 changes: 4 additions & 4 deletions test/default.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const test = require('ava')
const Fastify = require('fastify')
const S = require('fluent-json-schema').default
import test from 'ava'
import Fastify from 'fastify'
import S from 'fluent-json-schema'

const noAdditionalProperties = require('../fnap.js')
import noAdditionalProperties from '../fnap.mjs'

test('default', async t => {
t.plan(2)
Expand Down
6 changes: 3 additions & 3 deletions test/defined.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const test = require('ava')
const Fastify = require('fastify')
import test from 'ava'
import Fastify from 'fastify'

const noAdditionalProperties = require('../fnap.js')
import noAdditionalProperties from '../fnap.mjs'

test('defined', async t => {
t.plan(2)
Expand Down
6 changes: 3 additions & 3 deletions test/disabled.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const test = require('ava')
const Fastify = require('fastify')
import test from 'ava'
import Fastify from 'fastify'

const noAdditionalProperties = require('../fnap.js')
import noAdditionalProperties from '../fnap.mjs'

test('disabled', async t => {
t.plan(2)
Expand Down
26 changes: 13 additions & 13 deletions test/undefined.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
const test = require('ava')
const Fastify = require('fastify')
import test from 'ava'
import Fastify from 'fastify'

const noAdditionalProperties = require('../fnap.js')
import noAdditionalProperties from '../fnap.mjs'

test('undefined', async t => {
t.plan(2)

const fastify = Fastify()
t.teardown(() => fastify.close())

await fastify.register(noAdditionalProperties, {
body: true,
headers: true,
params: true,
query: true
})

fastify.route({
method: 'POST',
url: '/foo/:a/:b',
handler (request, reply) {
reply.send({
async handler (request) {
return {
body: request.body,
headers: request.headers,
params: request.params,
query: request.query
})
}
}
})

await fastify.register(noAdditionalProperties, {
body: true,
headers: true,
params: true,
query: true
})

const response = await fastify.inject({
method: 'POST',
url: '/foo/0/1?a=0&b=1',
Expand Down

0 comments on commit 7b347db

Please sign in to comment.