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/.github/workflows/main.yml b/.github/workflows/main.yaml similarity index 67% rename from .github/workflows/main.yml rename to .github/workflows/main.yaml index 28e9f44..2205bb0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yaml @@ -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 }} 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..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. @@ -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..adca06a 100644 --- a/lib/http.js +++ b/lib/http.js @@ -1,14 +1,27 @@ /*! - * 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 {example} from '../schemas/bedrock-template.js'; +import {namespace} from './config.js'; +import {createValidateMiddleware as validate} from '@bedrock/validation'; + +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..1abd08c --- /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); 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..401e6f4 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": "^51.4.1", + "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..39b661a --- /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() + } +}; 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..6ea7fb7 100644 --- a/test/package.json +++ b/test/package.json @@ -1,31 +1,37 @@ { - "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.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" }, - "nyc": { + "c8": { "excludeNodeModules": false, "include": [ - "node_modules/bedrock-module-template-http/**" + "node_modules/@bedrock/module-template-http/**" ], "exclude": [ - "node_modules/bedrock-module-template-http/node_modules/**" + "node_modules/@bedrock/module-template-http/node_modules/**" + ], + "reporter": [ + "lcov", + "text-summary", + "text" ] } } diff --git a/test/test.config.js b/test/test.config.js index 2bd6421..d871495 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; diff --git a/test/test.js b/test/test.js index cad3ac1..1c8ac8e 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();