json-rest-api 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +16 -0
- package/LICENSE +674 -0
- package/README.md +655 -309
- package/RELATIONSHIPS.md +1013 -0
- package/TODO_rest-api-plugin-extras.js +142 -0
- package/connectors_ideas/express-plugin.js +120 -0
- package/connectors_ideas/fastify.js +219 -0
- package/connectors_ideas/http-plugin.js +138 -0
- package/connectors_ideas/koa-plugin.js +113 -0
- package/docs/fileUploads.md +722 -0
- package/docs/mini-guides/file-uploads-complete.md +159 -0
- package/examples/.gitkeep +0 -0
- package/examples/express-example.js +262 -0
- package/examples/express-plugin.js +366 -0
- package/examples/file-upload-example.js +205 -0
- package/examples/filename-strategies.js +140 -0
- package/examples/knex-basic.js +66 -0
- package/index.js +31 -56
- package/lib/payload-validators.js +685 -0
- package/lib/rest-api-errors.js +58 -0
- package/package.json +41 -14
- package/payloadValdators.js +686 -0
- package/plugins/core/connectors/express-plugin.js +449 -0
- package/plugins/core/connectors/http-plugin.js +450 -0
- package/plugins/core/file-handling-plugin.js +338 -0
- package/plugins/core/lib/busboy-detector.js +167 -0
- package/plugins/core/lib/formidable-detector.js +130 -0
- package/plugins/core/rest-api-knex-plugin.js +200 -0
- package/plugins/core/rest-api-plugin.js +1419 -0
- package/plugins/storage/local-storage.js +218 -0
- package/plugins/storage/s3-storage.js +125 -0
- package/test-examples/package-lock.json +906 -0
- package/test-examples/package.json +15 -0
- package/test-examples/test-basic-api.js +11 -0
- package/test-examples/test-books-resource.js +116 -0
- package/test-examples/test-complete-file-upload.js +140 -0
- package/test-examples/test-debug.js +28 -0
- package/test-examples/test-express-plugin.js +177 -0
- package/test-examples/test-express-simple.js +28 -0
- package/test-examples/test-express-with-import.js +71 -0
- package/test-examples/test-file-upload.js +132 -0
- package/test-examples/test-http-plugin.js +159 -0
- package/test-examples/test-programmatic-api.js +188 -0
- package/test-examples/test-rest-api-plugin.js +13 -0
- package/TODO.todo +0 -113
- package/api.js +0 -628
- package/docs/API.md +0 -1306
- package/docs/FIXES_APPLIED.md +0 -59
- package/docs/GUIDES.md +0 -1792
- package/docs/RESOURCES_IN_EXPRESS.md +0 -456
- package/docs/example-versioning.js +0 -168
- package/docs/example.js +0 -195
- package/plugins/API guide.md +0 -364
- package/plugins/api-registry.js +0 -142
- package/plugins/api-versioning.md +0 -135
- package/plugins/http.js +0 -273
- package/plugins/logging.js +0 -255
- package/plugins/memory.js +0 -182
- package/plugins/mysql.js +0 -753
- package/plugins/openapi.js +0 -401
- package/plugins/positioning.js +0 -220
- package/plugins/security.js +0 -261
- package/plugins/validation.js +0 -173
- package/plugins/versioning.js +0 -288
- package/resource-helper.js +0 -79
- package/schema.js +0 -481
- package/test-basic.js +0 -107
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Api } from 'hooked-api';
|
|
2
|
+
import { RestApiPlugin, RestApiKnexPlugin, ExpressPlugin } from 'json-rest-api';
|
|
3
|
+
import knex from 'knex';
|
|
4
|
+
import express from 'express';
|
|
5
|
+
|
|
6
|
+
// Create a Knex instance (using SQLite for simplicity)
|
|
7
|
+
const db = knex({
|
|
8
|
+
client: 'sqlite3',
|
|
9
|
+
connection: {
|
|
10
|
+
filename: ':memory:'
|
|
11
|
+
},
|
|
12
|
+
useNullAsDefault: true
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// Create the API
|
|
16
|
+
const api = new Api({
|
|
17
|
+
name: 'library-api',
|
|
18
|
+
version: '1.0.0'
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// Use plugins
|
|
22
|
+
api.use(RestApiPlugin);
|
|
23
|
+
api.use(RestApiKnexPlugin, {
|
|
24
|
+
knex: { knex: db } // Pass the knex instance
|
|
25
|
+
});
|
|
26
|
+
api.use(ExpressPlugin);
|
|
27
|
+
|
|
28
|
+
// Create the books table
|
|
29
|
+
await db.schema.createTable('books', (table) => {
|
|
30
|
+
table.increments('id');
|
|
31
|
+
table.string('title').notNullable();
|
|
32
|
+
table.string('author').notNullable();
|
|
33
|
+
table.integer('year');
|
|
34
|
+
table.string('isbn');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Define the books resource
|
|
38
|
+
api.addResource('books', {
|
|
39
|
+
schema: {
|
|
40
|
+
title: { type: 'string', required: true },
|
|
41
|
+
author: { type: 'string', required: true },
|
|
42
|
+
year: { type: 'number' },
|
|
43
|
+
isbn: { type: 'string' }
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// Insert some sample data
|
|
48
|
+
await db('books').insert([
|
|
49
|
+
{ title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', year: 1925, isbn: '978-0-7432-7356-5' },
|
|
50
|
+
{ title: '1984', author: 'George Orwell', year: 1949, isbn: '978-0-452-28423-4' },
|
|
51
|
+
{ title: 'To Kill a Mockingbird', author: 'Harper Lee', year: 1960, isbn: '978-0-06-112008-4' }
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
// Create Express app
|
|
55
|
+
const app = express();
|
|
56
|
+
api.express.mount(app);
|
|
57
|
+
|
|
58
|
+
// Start the server
|
|
59
|
+
app.listen(3000, () => {
|
|
60
|
+
console.log('Library API with Knex running at http://localhost:3000/api');
|
|
61
|
+
console.log('');
|
|
62
|
+
console.log('Try these commands:');
|
|
63
|
+
console.log(' curl http://localhost:3000/api/books');
|
|
64
|
+
console.log(' curl http://localhost:3000/api/books/1');
|
|
65
|
+
console.log(' curl -X POST http://localhost:3000/api/books -H "Content-Type: application/json" -d \'{"data":{"type":"books","attributes":{"title":"New Book","author":"New Author","year":2024}}}\'');
|
|
66
|
+
});
|
package/index.js
CHANGED
|
@@ -1,61 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* JSON REST API - REST API plugin for hooked-api with JSON:API compliance
|
|
3
|
+
*
|
|
4
|
+
* This package provides:
|
|
5
|
+
* - REST API plugin that adds query, get, post, put, patch, delete methods to scopes
|
|
6
|
+
* - Connector plugins for various HTTP frameworks (Express, etc.)
|
|
7
|
+
* - JSON:API compliant request/response handling
|
|
8
|
+
* - Comprehensive error handling and validation
|
|
9
|
+
*/
|
|
4
10
|
|
|
5
|
-
//
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
8
|
-
export { HTTPPlugin } from './plugins/http.js';
|
|
9
|
-
export { ValidationPlugin } from './plugins/validation.js';
|
|
10
|
-
export { PositioningPlugin } from './plugins/positioning.js';
|
|
11
|
-
export { VersioningPlugin } from './plugins/versioning.js';
|
|
12
|
-
export { SecurityPlugin } from './plugins/security.js';
|
|
13
|
-
export { LoggingPlugin } from './plugins/logging.js';
|
|
14
|
-
export { OpenAPIPlugin } from './plugins/openapi.js';
|
|
15
|
-
export { ApiRegistryPlugin } from './plugins/api-registry.js';
|
|
11
|
+
// Core plugins
|
|
12
|
+
export { RestApiPlugin } from './plugins/core/rest-api-plugin.js';
|
|
13
|
+
export { FileHandlingPlugin } from './plugins/core/file-handling-plugin.js';
|
|
16
14
|
|
|
17
|
-
//
|
|
18
|
-
|
|
19
|
-
import { MemoryPlugin } from './plugins/memory.js';
|
|
20
|
-
import { MySQLPlugin } from './plugins/mysql.js';
|
|
21
|
-
import { HTTPPlugin } from './plugins/http.js';
|
|
22
|
-
import { ValidationPlugin } from './plugins/validation.js';
|
|
23
|
-
import { PositioningPlugin } from './plugins/positioning.js';
|
|
24
|
-
import { VersioningPlugin } from './plugins/versioning.js';
|
|
15
|
+
// Database plugins
|
|
16
|
+
export { RestApiKnexPlugin } from './plugins/core/rest-api-knex-plugin.js';
|
|
25
17
|
|
|
26
|
-
//
|
|
27
|
-
export
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (options.validation !== false) {
|
|
32
|
-
api.use(ValidationPlugin, options.validation);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (options.storage === 'memory') {
|
|
36
|
-
api.use(MemoryPlugin, options.memory);
|
|
37
|
-
} else if (options.storage === 'mysql') {
|
|
38
|
-
api.use(MySQLPlugin, options.mysql);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Auto-add HTTP plugin if http options are provided
|
|
42
|
-
if (options.http) {
|
|
43
|
-
api.use(HTTPPlugin, options.http);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (options.positioning) {
|
|
47
|
-
api.use(PositioningPlugin, options.positioning);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (options.versioning) {
|
|
51
|
-
api.use(VersioningPlugin, options.versioning);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return api;
|
|
55
|
-
}
|
|
18
|
+
// Connector plugins
|
|
19
|
+
export { ExpressPlugin } from './plugins/core/connectors/express-plugin.js';
|
|
20
|
+
export { HttpPlugin } from './plugins/core/connectors/http-plugin.js';
|
|
21
|
+
// Future: export { FastifyPlugin } from './plugins/core/connectors/fastify-plugin.js';
|
|
22
|
+
// Future: export { KoaPlugin } from './plugins/core/connectors/koa-plugin.js';
|
|
56
23
|
|
|
57
|
-
//
|
|
58
|
-
export {
|
|
24
|
+
// Storage plugins for file handling
|
|
25
|
+
export { LocalStorage } from './plugins/storage/local-storage.js';
|
|
26
|
+
export { S3Storage } from './plugins/storage/s3-storage.js';
|
|
59
27
|
|
|
60
|
-
//
|
|
61
|
-
export
|
|
28
|
+
// Error classes for consumers who need them
|
|
29
|
+
export {
|
|
30
|
+
RestApiValidationError,
|
|
31
|
+
RestApiResourceError,
|
|
32
|
+
RestApiPayloadError
|
|
33
|
+
} from './lib/rest-api-errors.js';
|
|
34
|
+
|
|
35
|
+
// Re-export everything for backward compatibility
|
|
36
|
+
export * from './lib/rest-api-errors.js';
|