Skip to content

Commit

Permalink
README updated
Browse files Browse the repository at this point in the history
  • Loading branch information
lolu-sholar committed Apr 13, 2024
1 parent 5da9830 commit 00b5001
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 31 deletions.
132 changes: 130 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,130 @@
# syntaxe-express
An express.js middleware built on the original syntaxe data query library.
<img src="assets/logo.svg" width="180px"/>
<br/>
Syntaxe middleware for Express.js
<br/>
<br/>

[![MIT licensed](https://img.shields.io/badge/license-MIT-0091F7)](./LICENSE)
![NPM Version](https://img.shields.io/badge/npm-v1.1.0-D50100)
![Top Language](https://img.shields.io/badge/javascript-100%25-F0DC4E)

<br/>

_Syntaxe is a declarative data querying library inspired by graphql._

[PLEASE REFER TO THE ORIGINAL SYNTAXE DOCUMENTATION FOR A MORE DETAILED COVERAGE OF THE SYNTAXE SCHEMA AND ITS OPERATORS.](https://github.com/lolu-sholar/syntaxe/blob/master/README.md)

Syntaxe Express is a middleware built on the original syntaxe engine to support declarative data fetching and querying when building server-side applications using express.js.

# Installation

## Setup

```bash
npm install syntaxe-express
```

# Example

### Server

```js
import express from 'express';
import SyntaxeIO from 'syntaxe-express';

import appUsers from './data/app-users.js';

const app = express();

////////////////////////////
// Add syntaxe middleware //
////////////////////////////
SyntaxeIO.init({
enabled: true,
app: app
});

app.get('/', (req, res) => {
res.status(200).json({
message: 'This is a syntaxe-enabled express.js application.'
});
});

app.get('/users', (req, res) => {
res.status(200).json(appUsers);
});

const port = 8000;

app.listen(port, () =>
console.log(`Express app listening on port 8000.`));

```

### Client

```js
const response = await fetch('http://127.0.0.1:8000/users', {
method: 'GET',
headers: {
'Syntaxe-Resolve-Schema': btoa(`{
id
fullName [as:"name"]
package? [ne:"free"]
} [first:5]`)
}
});
const result = await response.json();

/*
Response Headers:
Syntaxe-Enabled: true
Syntaxe-Schema-Resolved: true
Response Data:
[
{ id: 3, name: 'Person 3' },
{ id: 4, name: 'Person 4' },
{ id: 6, name: 'Person 6' },
{ id: 8, name: 'Person 8' },
{ id: 10, name: 'Person 10' }
]
*/
```

# Usage

## Availability

The first thing to look out for when communicating with a Syntaxe-enabled server application is the availability header `Syntaxe-Enabled`, which indicates whether Syntaxe is enabled for the application and ready to process queries sent from the client or not.

The Syntaxe middleware always returns the availability header `Syntaxe-Enabled` once hooked to your express application.

The value of the header is the value set to the `enabled` config property.

## Request

When making a query request from a client, there are two important commandments to note 😃:
- You shall include the header that holds your query schema - `Syntaxe-Resolve-Schema`.
- You shall convert your query schema to a base64 string, which should be the value of the header.

## Response

When a Syntaxe-enabled server returns a response, it returns one or two additional headers. The number of headers returned is based on the status of the query operation.

- When the query is successful, the additional header returned is `Syntaxe-Schema-Resolved` which has the value `true`.
- When the query fails, the two additional headers returned are `Syntaxe-Schema-Resolved` which has the value `false`, and also `Syntaxe-Schema-Resolved-Error` which contains some information on why the query failed.

# Support and Feedback

If you find any bugs or inconsistency, please submit an issue here in GitHub.

If you have any issues using the library, please contact me by email [[email protected]](mailto:[email protected]) with the subject 'Problem Using Syntaxe-Express'.

You are welcome to contribute or participate in making the library better.

_NOTE: Development of this library in various technologies, such as PHP, C#, Java, Python, and others, is currently ongoing, with support for both standalone and REST API usage._

# License

[The MIT License (MIT)](LICENSE)
23 changes: 15 additions & 8 deletions assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion dist/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/lib/engine.min.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions example/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import SyntaxeIO from '../../dist/index.min.js';

const app = express();

///////////////////////////
// Add syntaxe io object //
///////////////////////////
SyntaxeIO.apply({
////////////////////////////
// Add syntaxe middleware //
////////////////////////////
SyntaxeIO.init({
enabled: true,
app: app
});

app.get('/', (req, res) => {
res.status(200).json({
message: 'Welcome to the syntaxe-express example.'
message: 'This is a syntaxe-enabled express.js application.'
});
});

Expand Down
11 changes: 8 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { flags, scanDirectives, filterSchema, walkThroughHandler } from './lib/e

const SyntaxeIO = new Object();

SyntaxeIO.apply = (config = null) => {
SyntaxeIO.init = (config = null) => {
if (!config || !config.app)
throw new Error('No app detected.');

Expand All @@ -16,15 +16,20 @@ const SyntaxeRequestGate = class {
}

async #request(req, res, next) {
const { resolve, schema, client } = scanDirectives(req);
const { resolve, schema, client } = scanDirectives(req, res);

const enabledStatus = req.app.get('syntaxeEnabledStatus');

res.set('Syntaxe-Enabled', enabledStatus);

if (enabledStatus && resolve) {
res.syntaxeSchema = await filterSchema(schema);
new SyntaxeResponseGate(res);
if (res.syntaxeSchema.status)
new SyntaxeResponseGate(res);
else {
res.set('Syntaxe-Schema-Resolved', false);
res.set('Syntaxe-Schema-Resolved-Error', 'Query failed. Check your schema and try again.');
}
}

next();
Expand Down
Loading

0 comments on commit 00b5001

Please sign in to comment.