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

Restructure + nodenext #145

Merged
merged 7 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -14,4 +14,5 @@ jobs:
test:
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
with:
lint: true
license-check: true
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 2 additions & 3 deletions .taprc
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
ts: false
jsx: false
flow: false
files:
- 'test/**/*.test.js'
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const accepts = require('accepts')

const fp = require('fastify-plugin')

const acceptsObjectSymbol = Symbol('acceptsObject')
const acceptsObjectSymbol = Symbol.for('acceptsObject')
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved

const methodNames = [
'charset', 'charsets',
Expand All @@ -27,9 +27,7 @@ function replyAcceptMethod () {
return this.request[acceptsObjectSymbol]
}

function fastifyAcceptHeader (fastify, options, done) {
const decorateReplyToo = options.decorateReply

function fastifyAccepts (fastify, options, done) {
fastify.decorateRequest('accepts', acceptsMethod)

methodNames.forEach(methodName => {
Expand All @@ -40,7 +38,7 @@ function fastifyAcceptHeader (fastify, options, done) {
})
})

if (decorateReplyToo) {
if (options.decorateReply) {
fastify.decorateReply('requestAccepts', replyAcceptMethod)

methodNames.forEach(methodName => {
Expand All @@ -56,7 +54,9 @@ function fastifyAcceptHeader (fastify, options, done) {
done()
}

module.exports = fp(fastifyAcceptHeader, {
module.exports = fp(fastifyAccepts, {
fastify: '4.x',
name: '@fastify/accepts'
})
module.exports.default = fastifyAccepts
module.exports.fastifyAccepts = fastifyAccepts
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "4.0.1",
"description": "Add accept parser to fastify",
"main": "index.js",
"types": "types/index.d.ts",
"devDependencies": {
"@fastify/pre-commit": "^2.0.2",
"@types/accepts": "^1.3.5",
Expand All @@ -20,10 +21,9 @@
},
"scripts": {
"lint": "standard | snazzy",
"test": "npm run lint && npm run unit && npm run typescript",
"test:ci": "npm run lint && tap test.js --coverage-report=lcovonly && npm run typescript",
"typescript": "tsd",
"unit": "tap --100 test.js"
"test": "npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:unit": "tap"
},
"repository": {
"type": "git",
Expand All @@ -37,5 +37,9 @@
"homepage": "https://github.com/fastify/fastify-accepts#readme",
"publishConfig": {
"access": "public"
}
},
"pre-commit": [
"lint",
"test"
]
}
6 changes: 3 additions & 3 deletions test.js → test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const test = require('tap').test

const plugin = require('./')
const fastifyAccepts = require('..')

const request = require('request')
const Fastify = require('fastify')
Expand Down Expand Up @@ -93,7 +93,7 @@ test('accept header', t => {
t.plan(testCases.length)

const fastify = Fastify()
fastify.register(plugin, { decorateReply: true })
fastify.register(fastifyAccepts, { decorateReply: true })

t.teardown(fastify.close.bind(fastify))

Expand Down Expand Up @@ -139,7 +139,7 @@ test('accept header', t => {

test('no reply decorator', async function (t) {
const fastify = Fastify()
fastify.register(plugin, { decorateReply: false })
fastify.register(fastifyAccepts, { decorateReply: false })
await fastify.ready()

const methodNames = [
Expand Down
17 changes: 11 additions & 6 deletions index.d.ts → types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Accepts } from "accepts"
import { FastifyPlugin } from "fastify"
import { FastifyPluginCallback } from "fastify"

declare module "fastify" {
interface FastifyRequest extends Accepts {
Expand All @@ -18,11 +18,16 @@ declare module "fastify" {
requestTypes: Accepts["types"]
}
}
type FastifyAccepts = FastifyPluginCallback<fastifyAccepts.FastifyAcceptsOptions>

export interface FastifyAcceptsOptions {
decorateReply: boolean
}
declare namespace fastifyAccepts {
export interface FastifyAcceptsOptions {
decorateReply: boolean
}

declare const fastifyAccepts: FastifyPlugin<FastifyAcceptsOptions>
export const fastifyAccepts: FastifyAccepts
export { fastifyAccepts as default }
}

export default fastifyAccepts
declare function fastifyAccepts(...params: Parameters<FastifyAccepts>): ReturnType<FastifyAccepts>
export = fastifyAccepts
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion index.test-d.ts → types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fastify from "fastify"
import accepts from "."
import accepts from ".."

const app = fastify()

Expand Down