From 8142781e332481213c56741528ee4d995532ab47 Mon Sep 17 00:00:00 2001 From: Tyler Olson Date: Mon, 21 Jul 2025 12:33:00 -0400 Subject: [PATCH 1/7] Update template. --- .eslintrc.cjs | 15 +++++++++++++++ .eslintrc.js | 7 ------- .gitignore | 2 +- .npmrc | 1 + README.md | 4 ++-- lib/config.js | 9 ++++----- lib/http.js | 24 ++++++++++++++++++------ lib/index.js | 9 ++++----- lib/logger.js | 7 +++++++ lib/main.js | 7 ------- package.json | 25 +++++++++++++++---------- schemas/bedrock-template.js | 14 ++++++++++++++ 12 files changed, 81 insertions(+), 43 deletions(-) create mode 100644 .eslintrc.cjs delete mode 100644 .eslintrc.js create mode 100644 .npmrc create mode 100644 lib/logger.js delete mode 100644 lib/main.js create mode 100644 schemas/bedrock-template.js diff --git a/.eslintrc.cjs b/.eslintrc.cjs new file mode 100644 index 0000000..a941733 --- /dev/null +++ b/.eslintrc.cjs @@ -0,0 +1,15 @@ +module.exports = { + root: true, + env: { + node: true + }, + extends: [ + 'digitalbazaar', + 'digitalbazaar/jsdoc', + 'digitalbazaar/module' + ], + ignorePatterns: ['node_modules/'], + rules: { + 'unicorn/prefer-node-protocol': 'error' + } +}; diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 2297f74..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1,7 +0,0 @@ -module.exports = { - root: true, - env: { - node: true - }, - extends: 'digitalbazaar' -}; diff --git a/.gitignore b/.gitignore index cc36171..8c61364 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ *.log *.sw[nop] *~ -.nyc_output .project .settings .vscode @@ -9,3 +8,4 @@ TAGS coverage node_modules reports +package-lock.json \ No newline at end of file diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..e9ee3cb --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +legacy-peer-deps=true \ No newline at end of file diff --git a/README.md b/README.md index c6b1fa1..70108f2 100644 --- a/README.md +++ b/README.md @@ -25,14 +25,14 @@ TBD ## Install -- Node.js 12+ is required. +- Node.js 20+ is required. ### NPM To install via NPM: ``` -npm install --save bedrock-module-template-http +npm install --save @bedrock/module-template-http ``` ### Development diff --git a/lib/config.js b/lib/config.js index f6120da..2791170 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,13 +1,12 @@ /*! - * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. + * Copyright (c) 2025 Digital Bazaar, Inc. All rights reserved. */ -import bedrock from 'bedrock'; +import * as bedrock from '@bedrock/core'; const {config} = bedrock; -const namespace = 'module-template-http'; +export const namespace = 'module-template-http'; const cfg = config[namespace] = {}; -const basePath = '/foo'; cfg.routes = { - basePath + basePath: '/foo' }; diff --git a/lib/http.js b/lib/http.js index a57c31d..c4a8cd1 100644 --- a/lib/http.js +++ b/lib/http.js @@ -1,14 +1,26 @@ /*! - * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. + * Copyright (c) 2025 Digital Bazaar, Inc. All rights reserved. */ -import {asyncHandler} from 'bedrock-express'; -import bedrock from 'bedrock'; -const {config} = bedrock; +import * as bedrock from '@bedrock/core'; +import {asyncHandler} from '@bedrock/express'; +import {createValidateMiddleware as validate} from '@bedrock/validation'; +import {example} from '../schemas/bedrock-template.js' + +const {util: {BedrockError}} = bedrock; bedrock.events.on('bedrock-express.configure.routes', app => { - const {routes} = config['module-template-http']; + const {config} = bedrock; + const {routes} = config[namespace]; + app.post( routes.basePath, + validate({bodySchema: example}), asyncHandler(async (/*req, res*/) => { - })); + throw new BedrockError( + 'Not implemented.', { + name: 'NotFoundError', + details: {httpStatusCode: 501, public: true} + }); + }) + ); }); diff --git a/lib/index.js b/lib/index.js index e731f70..bcfaf5e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,8 +1,7 @@ /*! - * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. + * Copyright (c) 2025 Digital Bazaar, Inc. All rights reserved. */ -'use strict'; +import '@bedrock/express'; -// translate `main.js` to CommonJS -require = require('esm')(module); -module.exports = require('./main.js'); +import './config.js'; +import './http.js'; diff --git a/lib/logger.js b/lib/logger.js new file mode 100644 index 0000000..5401336 --- /dev/null +++ b/lib/logger.js @@ -0,0 +1,7 @@ +/*! + * Copyright (c) 2025 Digital Bazaar, Inc. All rights reserved. + */ +import {loggers} from '@bedrock/core'; +import {namespace} from './config.js'; + +export const logger = loggers.get('app').child(namespace); \ No newline at end of file diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7e65f16..0000000 --- a/lib/main.js +++ /dev/null @@ -1,7 +0,0 @@ -/*! - * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. - */ -import 'bedrock-express'; - -import './config.js'; -import './http.js'; diff --git a/package.json b/package.json index 8265c7b..9917d50 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,13 @@ { - "name": "bedrock-module-template-http", + "name": "@bedrock/module-template-http", "version": "0.0.1-0", + "type": "module", "description": "Bedrock HTTP API", - "main": "./lib", + "main": "./lib/index.js", + "files": [ + "lib/**/*.js", + "schemas/**/*.js" + ], "scripts": { "lint": "eslint ." }, @@ -22,21 +27,21 @@ "url": "https://github.com/digitalbazaar/bedrock-module-template-http/issues" }, "homepage": "https://github.com/digitalbazaar/bedrock-module-template-http", - "dependencies": { - "esm": "^3.2.25" - }, "peerDependencies": { - "bedrock": "^4.4.0", - "bedrock-express": "^5.0.1" + "@bedrock/core": "^6.3.0", + "@bedrock/express": "^8.3.2", + "@bedrock/validation": "^7.1.1" }, "directories": { "lib": "./lib" }, "devDependencies": { - "eslint": "^7.32.0", - "eslint-config-digitalbazaar": "^2.8.0" + "eslint": "^8.57.1", + "eslint-config-digitalbazaar": "^5.2.0", + "eslint-plugin-jsdoc": "^50.6.8", + "eslint-plugin-unicorn": "^56.0.1" }, "engines": { - "node": ">=12" + "node": ">=20" } } diff --git a/schemas/bedrock-template.js b/schemas/bedrock-template.js new file mode 100644 index 0000000..5af177e --- /dev/null +++ b/schemas/bedrock-template.js @@ -0,0 +1,14 @@ +/*! + * Copyright (c) 2025 Digital Bazaar, Inc. All rights reserved. + */ +import {schemas} from '@bedrock/validation'; + +export const example = { + title: 'Example', + type: 'object', + required: ['id'], + additionalProperties: false, + properties: { + id: schemas.identifier() + } +} \ No newline at end of file From 765cca37605bdcf91b2987eb42acaae240684bd6 Mon Sep 17 00:00:00 2001 From: Tyler Olson Date: Mon, 21 Jul 2025 12:33:09 -0400 Subject: [PATCH 2/7] Update actions. --- .github/workflows/main.yml | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 28e9f44..2205bb0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -8,11 +8,11 @@ jobs: timeout-minutes: 10 strategy: matrix: - node-version: [14.x] + node-version: [22.x] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: npm install @@ -22,18 +22,13 @@ jobs: needs: [lint] runs-on: ubuntu-latest timeout-minutes: 10 - services: - mongodb: - image: mongo:4.2 - ports: - - 27017:27017 strategy: matrix: - node-version: [12.x, 14.x] + node-version: [20.x, 22.x] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: | @@ -47,18 +42,13 @@ jobs: needs: [test-node] runs-on: ubuntu-latest timeout-minutes: 10 - services: - mongodb: - image: mongo:4.2 - ports: - - 27017:27017 strategy: matrix: - node-version: [14.x] + node-version: [22.x] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 + uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: | @@ -69,7 +59,8 @@ jobs: cd test npm run coverage-ci - name: Upload coverage to Codecov - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v5 with: - file: ./test/coverage/lcov.info + files: ./test/coverage/lcov.info fail_ci_if_error: true + token: ${{ secrets.CODECOV_TOKEN }} From 3865f1c94ca23528575dc26cc1a72b652515e26d Mon Sep 17 00:00:00 2001 From: Tyler Olson Date: Mon, 21 Jul 2025 12:33:17 -0400 Subject: [PATCH 3/7] Update tests. --- test/mocha/.eslintrc | 9 --------- test/mocha/.eslintrc.cjs | 9 +++++++++ test/mocha/10-api.js | 3 +-- test/package.json | 39 ++++++++++++++++++++++----------------- test/test.config.js | 17 +++++------------ test/test.js | 13 +++++-------- 6 files changed, 42 insertions(+), 48 deletions(-) delete mode 100644 test/mocha/.eslintrc create mode 100644 test/mocha/.eslintrc.cjs diff --git a/test/mocha/.eslintrc b/test/mocha/.eslintrc deleted file mode 100644 index f9f5022..0000000 --- a/test/mocha/.eslintrc +++ /dev/null @@ -1,9 +0,0 @@ -{ - "env": { - "mocha": true - }, - "globals": { - "assertNoError": true, - "should": true - } -} diff --git a/test/mocha/.eslintrc.cjs b/test/mocha/.eslintrc.cjs new file mode 100644 index 0000000..b679b05 --- /dev/null +++ b/test/mocha/.eslintrc.cjs @@ -0,0 +1,9 @@ +module.exports = { + env: { + mocha: true + }, + globals: { + assertNoError: true, + should: true + } +} \ No newline at end of file diff --git a/test/mocha/10-api.js b/test/mocha/10-api.js index 6157d46..8735aae 100644 --- a/test/mocha/10-api.js +++ b/test/mocha/10-api.js @@ -1,7 +1,6 @@ /* - * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. + * Copyright (c) 2025 Digital Bazaar, Inc. All rights reserved. */ - describe('api', () => { it('should work'); }); diff --git a/test/package.json b/test/package.json index 0105fc8..675c39c 100644 --- a/test/package.json +++ b/test/package.json @@ -1,31 +1,36 @@ { - "name": "bedrock-module-template-http-test", - "version": "0.0.1-0", + "name": "@bedrock/module-template-http-test", + "version": "1.0.0", "private": true, + "type": "module", "scripts": { "test": "node --preserve-symlinks test.js test", - "coverage": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text-summary npm test", - "coverage-ci": "cross-env NODE_ENV=test nyc --reporter=lcovonly npm test", - "coverage-report": "nyc report" + "coverage": "cross-env NODE_ENV=test c8 --reporter=lcov --reporter=text-summary npm test", + "coverage-ci": "cross-env NODE_ENV=test c8 --reporter=lcovonly npm test", + "coverage-report": "c8 report" }, "dependencies": { - "bedrock": "^4.4.0", - "bedrock-express": "^5.0.1", - "bedrock-https-agent": "^2.1.0", - "bedrock-module-template-http": "file:..", - "bedrock-mongodb": "^8.4.0", - "bedrock-server": "^3.0.0", - "bedrock-test": "^5.4.0", - "cross-env": "^7.0.3", - "nyc": "^15.1.0" + "@bedrock/core": "^6.1.3", + "@bedrock/express": "^8.3.0", + "@bedrock/https-agent": "^4.1.0", + "@bedrock/module-template-http": "file:..", + "@bedrock/server": "^5.1.0", + "@bedrock/test": "^8.2.0", + "c8": "^10.1.3", + "cross-env": "^7.0.3" }, - "nyc": { + "c8": { "excludeNodeModules": false, "include": [ - "node_modules/bedrock-module-template-http/**" + "node_modules/@bedrock/bedrock-recaptcha/**" ], "exclude": [ - "node_modules/bedrock-module-template-http/node_modules/**" + "node_modules/@bedrock/bedrock-recaptcha/node_modules/**" + ], + "reporter": [ + "lcov", + "text-summary", + "text" ] } } diff --git a/test/test.config.js b/test/test.config.js index 2bd6421..d4dfa3a 100644 --- a/test/test.config.js +++ b/test/test.config.js @@ -1,17 +1,10 @@ /* - * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. + * Copyright (c) 2025 Digital Bazaar, Inc. All rights reserved.. */ -'use strict'; +import {config} from '@bedrock/core'; +import path from 'node:path'; -const {config} = require('bedrock'); -const path = require('path'); - -// MongoDB -config.mongodb.name = 'bedrock_module_template_http_test'; -config.mongodb.dropCollections.onInit = true; -config.mongodb.dropCollections.collections = []; - -config.mocha.tests.push(path.join(__dirname, 'mocha')); +config.mocha.tests.push(path.join(import.meta.dirname, 'mocha')); // allow self-signed certs in test framework -config['https-agent'].rejectUnauthorized = false; +config['https-agent'].rejectUnauthorized = false; \ No newline at end of file diff --git a/test/test.js b/test/test.js index cad3ac1..ec070c8 100644 --- a/test/test.js +++ b/test/test.js @@ -1,12 +1,9 @@ /* - * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. + * Copyright (c) 2025 Digital Bazaar, Inc. All rights reserved. */ -'use strict'; +import * as bedrock from '@bedrock/core'; +import '@bedrock/https-agent'; +import '@bedrock/test'; -const bedrock = require('bedrock'); -require('bedrock-https-agent'); -require('bedrock-mongodb'); -require('bedrock-module-template-http'); - -require('bedrock-test'); +import "@bedrock/module-template-http"; bedrock.start(); From a6181d35c549a20f3b763d13036e105c3f78c52a Mon Sep 17 00:00:00 2001 From: Tyler Olson Date: Mon, 21 Jul 2025 12:56:01 -0400 Subject: [PATCH 4/7] Update test dependencies. --- test/package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/package.json b/test/package.json index 675c39c..3e9ddbe 100644 --- a/test/package.json +++ b/test/package.json @@ -10,12 +10,13 @@ "coverage-report": "c8 report" }, "dependencies": { - "@bedrock/core": "^6.1.3", - "@bedrock/express": "^8.3.0", + "@bedrock/core": "^6.3.0", + "@bedrock/express": "^8.3.2", "@bedrock/https-agent": "^4.1.0", "@bedrock/module-template-http": "file:..", "@bedrock/server": "^5.1.0", "@bedrock/test": "^8.2.0", + "@bedrock/validation": "^7.1.1", "c8": "^10.1.3", "cross-env": "^7.0.3" }, From a78b71870b8fbe844f676c02c9317a38f29837df Mon Sep 17 00:00:00 2001 From: Tyler Olson Date: Tue, 22 Jul 2025 10:08:51 -0400 Subject: [PATCH 5/7] Apply code review changes. Co-authored-by: David I. Lehn --- README.md | 6 +++--- package.json | 2 +- test/package.json | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 70108f2..6af8cc8 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# Bedrock HTTP API Module Template _(bedrock-module-template-http)_ +# Bedrock HTTP API Module Template _(@bedrock/module-template-http)_ -[![Build Status](https://img.shields.io/github/workflow/status/digitalbazaar/bedrock-module-template-http/Bedrock%20Node.js%20CI)](https://github.com/digitalbazaar/bedrock-module-template-http/actions?query=workflow%3A%22Bedrock+Node.js+CI%22) -[![NPM Version](https://img.shields.io/npm/v/bedrock-module-template-http.svg)](https://npm.im/bedrock-module-template-http) +[![Build Status](https://img.shields.io/github/actions/workflow/status/digitalbazaar/bedrock-module-temlpate-http/main.yaml)](https://github.com/digitalbazaar/bedrock-module-template-http/actions/workflows/main.yaml) +[![NPM Version](https://img.shields.io/npm/v/@bedrock/module-template-http.svg)](https://npm.im/@bedrock/module-template-http) > A template for HTTP API modules for Bedrock applications. diff --git a/package.json b/package.json index 9917d50..401e6f4 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "devDependencies": { "eslint": "^8.57.1", "eslint-config-digitalbazaar": "^5.2.0", - "eslint-plugin-jsdoc": "^50.6.8", + "eslint-plugin-jsdoc": "^51.4.1", "eslint-plugin-unicorn": "^56.0.1" }, "engines": { diff --git a/test/package.json b/test/package.json index 3e9ddbe..6ea7fb7 100644 --- a/test/package.json +++ b/test/package.json @@ -23,10 +23,10 @@ "c8": { "excludeNodeModules": false, "include": [ - "node_modules/@bedrock/bedrock-recaptcha/**" + "node_modules/@bedrock/module-template-http/**" ], "exclude": [ - "node_modules/@bedrock/bedrock-recaptcha/node_modules/**" + "node_modules/@bedrock/module-template-http/node_modules/**" ], "reporter": [ "lcov", From 62063cfcd7b0a7fd6dbc9e7ae224d621a9a0e40b Mon Sep 17 00:00:00 2001 From: Tyler Olson Date: Tue, 22 Jul 2025 10:10:10 -0400 Subject: [PATCH 6/7] Use `.yaml` for actions file extension. --- .github/workflows/{main.yml => main.yaml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{main.yml => main.yaml} (100%) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yaml similarity index 100% rename from .github/workflows/main.yml rename to .github/workflows/main.yaml From 31ee337527be3128d956540be962e8554853329d Mon Sep 17 00:00:00 2001 From: Tyler Olson Date: Tue, 22 Jul 2025 10:13:50 -0400 Subject: [PATCH 7/7] Lint. --- lib/http.js | 5 +++-- lib/logger.js | 2 +- schemas/bedrock-template.js | 2 +- test/test.config.js | 2 +- test/test.js | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/http.js b/lib/http.js index c4a8cd1..adca06a 100644 --- a/lib/http.js +++ b/lib/http.js @@ -3,8 +3,9 @@ */ import * as bedrock from '@bedrock/core'; import {asyncHandler} from '@bedrock/express'; +import {example} from '../schemas/bedrock-template.js'; +import {namespace} from './config.js'; import {createValidateMiddleware as validate} from '@bedrock/validation'; -import {example} from '../schemas/bedrock-template.js' const {util: {BedrockError}} = bedrock; @@ -14,7 +15,7 @@ bedrock.events.on('bedrock-express.configure.routes', app => { app.post( routes.basePath, - validate({bodySchema: example}), + validate({bodySchema: example}), asyncHandler(async (/*req, res*/) => { throw new BedrockError( 'Not implemented.', { diff --git a/lib/logger.js b/lib/logger.js index 5401336..1abd08c 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -4,4 +4,4 @@ import {loggers} from '@bedrock/core'; import {namespace} from './config.js'; -export const logger = loggers.get('app').child(namespace); \ No newline at end of file +export const logger = loggers.get('app').child(namespace); diff --git a/schemas/bedrock-template.js b/schemas/bedrock-template.js index 5af177e..39b661a 100644 --- a/schemas/bedrock-template.js +++ b/schemas/bedrock-template.js @@ -11,4 +11,4 @@ export const example = { properties: { id: schemas.identifier() } -} \ No newline at end of file +}; diff --git a/test/test.config.js b/test/test.config.js index d4dfa3a..d871495 100644 --- a/test/test.config.js +++ b/test/test.config.js @@ -7,4 +7,4 @@ import path from 'node:path'; config.mocha.tests.push(path.join(import.meta.dirname, 'mocha')); // allow self-signed certs in test framework -config['https-agent'].rejectUnauthorized = false; \ No newline at end of file +config['https-agent'].rejectUnauthorized = false; diff --git a/test/test.js b/test/test.js index ec070c8..1c8ac8e 100644 --- a/test/test.js +++ b/test/test.js @@ -5,5 +5,5 @@ import * as bedrock from '@bedrock/core'; import '@bedrock/https-agent'; import '@bedrock/test'; -import "@bedrock/module-template-http"; +import '@bedrock/module-template-http'; bedrock.start();