Skip to content

Commit

Permalink
samples
Browse files Browse the repository at this point in the history
  • Loading branch information
leandroandrade committed Mar 8, 2024
1 parent 115bfbf commit 1aa2414
Show file tree
Hide file tree
Showing 7 changed files with 7,477 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
env: {
browser: true,
commonjs: true,
es2021: true,
},
extends: ['airbnb-base'],
plugins: ['security'],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parserOptions: {
ecmaVersion: 11,
},
rules: {
'no-unused-vars': ['error', { argsIgnorePattern: 'next' }],
'no-unused-expressions': [2, { allowShortCircuit: true }],
'no-return-await': 'off',
'class-methods-use-this': 'off',
'no-shadow': 'off',
'arrow-body-style': 'off',
'no-param-reassign': 'off',
'no-underscore-dangle': 'off',
'global-require': 'off',
'arrow-parens': 'off',
},
};
140 changes: 140 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
.idea
node_modules
coverage
.env

# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc tests coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env*.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Storybook build outputs
.out
.storybook-out
storybook-static

# rollup.js default build output
dist/

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# Temporary folders
tmp/
temp/

# End of https://www.toptal.com/developers/gitignore/api/node

test-report.xml

.DS_Store

ztest
13 changes: 13 additions & 0 deletions headers-every-response/custom-route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const fp = require('fastify-plugin');

module.exports = fp(async (fastify) => {
fastify.addHook('onSend', async (request, reply) => {
reply.headers({
'x-custom-header': 'foo',
});
});

fastify.get('/custom-route', async (req, res) => {
return { custom: 'route' };
});
});
42 changes: 42 additions & 0 deletions headers-every-response/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const Fastify = require('fastify');
const { deepEqual, equal } = require('node:assert/strict');

(async () => {
const app = Fastify();

app.addHook('onSend', async (request, reply) => {
reply.headers({
'Cache-Control': 'no-store',
Pragma: 'no-cache',
});
});

await app.register(require('./custom-route'));

app.get('/', async (req, res) => {
return { hello: 'world' };
});

await app.listen({ port: 3000 });

const res = await app.inject({
method: 'GET',
url: '/',
});

deepEqual(res.statusCode, 200);
deepEqual(res.json(), { hello: 'world' });
equal(res.headers['cache-control'], 'no-store');
equal(res.headers.pragma, 'no-cache');

const resCustom = await app.inject({
method: 'GET',
url: '/custom-route',
});

deepEqual(resCustom.statusCode, 200);
deepEqual(resCustom.json(), { custom: 'route' });
equal(res.headers['x-custom-header'], 'foo');

await app.close();
})();
Loading

0 comments on commit 1aa2414

Please sign in to comment.