From 98f940540df42ca2b2554e8a3e504429d9124caa Mon Sep 17 00:00:00 2001 From: Fergus McDowall Date: Mon, 2 Sep 2024 14:13:36 +0200 Subject: [PATCH] WIP --- build/mirrorUnpkg.js | 4 ++-- build/openAPIDoc.js | 15 ++++++++------- package.json | 2 +- src/API.js | 20 +++++++++++++++++--- src/Norch.js | 2 +- test/general-test.js | 12 +++++++----- test/run-from-node-test.js | 2 +- www_root/openapi-norch-1.0.0.json | 16 +++++++++++++++- 8 files changed, 52 insertions(+), 21 deletions(-) diff --git a/build/mirrorUnpkg.js b/build/mirrorUnpkg.js index 191ad91..4fecc80 100644 --- a/build/mirrorUnpkg.js +++ b/build/mirrorUnpkg.js @@ -1,4 +1,4 @@ -const fs = require('fs') +import { writeFile } from 'node:fs/promises' const files = [ 'swagger-ui.css', @@ -10,6 +10,6 @@ Promise.all( files.map(f => fetch('https://unpkg.com/swagger-ui-dist@4.5.0/' + f) .then(res => res.text()) - .then(text => fs.promises.writeFile('www_root/' + f, text)) + .then(text => writeFile('www_root/' + f, text)) ) ) diff --git a/build/openAPIDoc.js b/build/openAPIDoc.js index 5df6f74..e4fd17a 100644 --- a/build/openAPIDoc.js +++ b/build/openAPIDoc.js @@ -1,6 +1,7 @@ -const fs = require('fs') -const swaggerJsdoc = require('swagger-jsdoc') -const norchVersion = require('../package.json').version +import { readFileSync, writeFileSync } from 'node:fs' +import swaggerJsdoc from 'swagger-jsdoc' + +const version = JSON.parse(readFileSync('./package.json', 'utf8')).version const swaggerDefinition = { openapi: '3.0.1', @@ -8,7 +9,7 @@ const swaggerDefinition = { title: 'Norch', description: 'This is the API documentation for `norch@' + - norchVersion + + version + '`. You can use this page to get familiar with what Norch can do. If you want to try out the API calls on a server other than `localhost`, fill out the server variable below in the input box labeled `URL`', contact: { email: 'fergus@norch.io' @@ -17,7 +18,7 @@ const swaggerDefinition = { // name: 'MIT', // url: 'https://github.com/fergiemcdowall/search-index/blob/master/LICENSE' // }, - version: norchVersion + version }, externalDocs: { description: 'Find out more about Swagger', @@ -281,8 +282,8 @@ const swaggerDefinition = { } } -fs.writeFileSync( - 'www_root/openapi-norch-' + norchVersion + '.json', +writeFileSync( + 'www_root/openapi-norch-' + version + '.json', JSON.stringify( swaggerJsdoc({ swaggerDefinition, diff --git a/package.json b/package.json index 7ac704b..6acfedd 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ }, "scripts": { "build-openapi-docs": "node build/mirrorUnpkg.js && node build/openAPIDoc.js && npm run lint", - "lint": "standard --fix src build", + "lint": "standard --fix src build test", "test": "export SANDBOX=test/sandbox && rm -rf $SANDBOX && mkdir $SANDBOX && date && tape test/*-test.js && npm run lint", "test-cli": "./bin/norch -p 9090 -i test/sandbox/norch-cli & sleep 5s && bats test/test.bats && kill $!", "start": "./bin/norch" diff --git a/src/API.js b/src/API.js index cff0edc..9c6b2f3 100644 --- a/src/API.js +++ b/src/API.js @@ -76,6 +76,8 @@ export class API { .DELETE(...this.params(req, 'ID')) .then(idxRes => this.sendJSONResponse(idxRes, res)) + // TODO: DELETE_RAW? + /** * @openapi * /DICTIONARY: @@ -137,6 +139,8 @@ export class API { .DOCUMENTS(...this.params(req, 'ID')) .then(b => this.sendJSONResponse(b, res)) + // TODO: DOCUMENT_VECTORS? + /** * @openapi * /EXPORT: @@ -514,10 +518,8 @@ export class API { ]).then(([LAST_UPDATED, CREATED, DOCUMENT_COUNT]) => this.sendJSONResponse( { - IS_ALIVE: true, + READY: true, DOCUMENT_COUNT, - // Update these lines when https://github.com/fergiemcdowall/search-index/issues/600 - // is rolled out CREATED: new Date(CREATED), LAST_UPDATED: new Date(LAST_UPDATED) }, @@ -525,6 +527,18 @@ export class API { ) ) + /** + * @openapi + * /READY: + * get: + * tags: [READ] + * summary: Is index ready? + * description: | + * Returns `{ READY: true }` when index is ready + * responses: + * 200: + * description: Index is ready + */ READY = (req, res) => this.ready ? this.sendJSONResponse({ READY: true }, res) diff --git a/src/Norch.js b/src/Norch.js index 60e7502..4d6d568 100644 --- a/src/Norch.js +++ b/src/Norch.js @@ -10,7 +10,7 @@ import { resolve, dirname } from 'path' import figlet from 'figlet' export class Norch { - constructor(ops = {}) { + constructor (ops = {}) { const defaultConfigFile = JSON.parse( readFileSync('./src/defaultConfig.json', 'utf8') ) diff --git a/test/general-test.js b/test/general-test.js index 8c32a91..c821604 100644 --- a/test/general-test.js +++ b/test/general-test.js @@ -55,13 +55,15 @@ test('start a norch', async t => { await fetch('http://localhost:3030/STATUS') .then(res => res.json()) .then(json => - t.isEquivalent( - ['IS_ALIVE', 'DOCUMENT_COUNT', 'CREATED', 'LAST_UPDATED'], - Object.keys(json) - ) + t.isEquivalent(Object.keys(json), [ + 'READY', + 'DOCUMENT_COUNT', + 'CREATED', + 'LAST_UPDATED' + ]) ) - .catch(t.error) + .catch(t.error) await fetch('http://localhost:3030/PUT', { method: 'POST', body: JSON.stringify([ diff --git a/test/run-from-node-test.js b/test/run-from-node-test.js index 115936e..5a93b55 100644 --- a/test/run-from-node-test.js +++ b/test/run-from-node-test.js @@ -23,7 +23,7 @@ test('start a norch', async t => { .then(res => res.json()) .then(json => t.isEquivalent( - ['IS_ALIVE', 'DOCUMENT_COUNT', 'CREATED', 'LAST_UPDATED'], + ['READY', 'DOCUMENT_COUNT', 'CREATED', 'LAST_UPDATED'], Object.keys(json) ) ) diff --git a/www_root/openapi-norch-1.0.0.json b/www_root/openapi-norch-1.0.0.json index e84d23a..3f2bb18 100644 --- a/www_root/openapi-norch-1.0.0.json +++ b/www_root/openapi-norch-1.0.0.json @@ -659,7 +659,7 @@ }, "responses": { "200": { - "description": "Successfully imported" + "description": "Successfully written" } } } @@ -774,6 +774,20 @@ } } } + }, + "/READY": { + "get": { + "tags": [ + "READ" + ], + "summary": "Is index ready?", + "description": "Returns `{ READY: true }` when index is ready\n", + "responses": { + "200": { + "description": "Index is ready" + } + } + } } } } \ No newline at end of file