Skip to content

Commit

Permalink
Added altair-koa-middleware package.
Browse files Browse the repository at this point in the history
Closes #617.
  • Loading branch information
imolorhe committed Apr 6, 2019
1 parent 272b5df commit 9350936
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/altair-koa-middleware/.gitignore
@@ -0,0 +1,2 @@
node_modules/
build/
1 change: 1 addition & 0 deletions packages/altair-koa-middleware/.npmignore
@@ -0,0 +1 @@
/scripts
21 changes: 21 additions & 0 deletions packages/altair-koa-middleware/LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) [year] [fullname]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
55 changes: 55 additions & 0 deletions packages/altair-koa-middleware/README.md
@@ -0,0 +1,55 @@
# altair-express-middleware

[![npm](https://img.shields.io/npm/v/altair-koa-middleware.svg)](https://www.npmjs.com/package/altair-koa-middleware)

This is a koa middleware for mounting an instance of altair GraphQL client.

### Installation
This is a node module and can be installed using npm:

```
npm install --save altair-koa-middleware
```

Alternatively, if you are using [`yarn`](https://yarnpkg.com/):

```
yarn add altair-koa-middleware
```

### Usage

```js
import Koa from 'koa';
import KoaRouter from 'koa-router';
import { createRouteExplorer } from 'altair-koa-middleware';
const app = new Koa();
const router = new KoaRouter();

createRouteExplorer({
url: '/altair',
router,
opts: {
endpoint: '/graphql',
subscriptionsEndpoint: `ws://localhost:4000/subscriptions`,
initialQuery: `{ getData { id name surname } }`,
},
});

app
.use(router.routes())
.use(router.allowedMethods());

app.listen(3500);

// ... the rest of your code ...
```

An instance of Altair GraphQL Client would be available at `/altair` of your server.

### Contributing
Everyone is welcome to contribute. See anything that needs improving, create an issue. And if you're up for it, create a PR! :D

### License

[MIT](../../LICENSE)
23 changes: 23 additions & 0 deletions packages/altair-koa-middleware/index.ts
@@ -0,0 +1,23 @@
'use strict';

import * as send from 'koa-send';
import { getDistDirectory, renderAltair, RenderOptions } from 'altair-static';

export const createRouteExplorer = ({ router, url, opts }: { router, url: string, opts: RenderOptions }) => {
router.get(url, async (ctx, next) => {

ctx.body = renderAltair({ baseURL: ctx.url.replace(/\/?$/, '/'), ...opts });

await next();
})

// Use the main favicon for my API subdomain.
router.get(`${url}/favicon.ico`, ctx => {
ctx.status = 301;
ctx.redirect(`/favicon.ico`);
})

router.get(`${url}/:path+`, async ctx => {
await send(ctx, ctx.params.path, { root: getDistDirectory() });
})
};
41 changes: 41 additions & 0 deletions packages/altair-koa-middleware/package.json
@@ -0,0 +1,41 @@
{
"name": "altair-koa-middleware",
"version": "2.1.5",
"description": "Koa middleware for altair graphql client",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"prepare-dist": "node scripts/prepare_dist.js",
"declarations": "tsc --declaration",
"prepare": "npm run declarations"
},
"engines": {
"node": ">= 6.9.1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/imolorhe/altair.git"
},
"keywords": [
"altair",
"graphql",
"express",
"middleware"
],
"author": "Samuel Imolorhe <samuelimolo4real@gmail.com> (https://sirmuel.design)",
"license": "MIT",
"bugs": {
"url": "https://github.com/imolorhe/altair/issues"
},
"homepage": "https://github.com/imolorhe/altair#readme",
"dependencies": {
"altair-static": "^2.1.5",
"koa-router": "^7.4.0",
"koa-send": "^5.0.0"
},
"devDependencies": {
"typescript": "^2.8.1"
},
"gitHead": "eaad78119e8966f659d2115ff28cf30f27f729bf"
}
25 changes: 25 additions & 0 deletions packages/altair-koa-middleware/scripts/prepare_dist.js
@@ -0,0 +1,25 @@
const ncp = require('ncp').ncp;
const path = require('path');
const fs = require('fs');

ncp.limit = 16;

const distSrc = path.join(__dirname, '../../../dist');
const distDestination = path.join(__dirname, '../dist');

const indexHtmlFile = path.join(distDestination, 'index.html');

ncp(distSrc, distDestination, function (err) {
if (err) {
return console.error(err);
}
console.log('Done copying dist folder!');

let indexHtmlStr = fs.readFileSync(indexHtmlFile, 'utf8');

// Adjust the base URL to be relative to the current path
indexHtmlStr = indexHtmlStr.replace('<base href="/">', '<base href="./">');

// Write the new string back to file
fs.writeFileSync(indexHtmlFile, indexHtmlStr, 'utf8');
});
16 changes: 16 additions & 0 deletions packages/altair-koa-middleware/tsconfig.json
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"outDir": "build/",
"rootDir": "./",
"declaration": true,
"target": "es5",
"module": "commonjs",
"lib": [
"dom",
"esnext"
],
"types": [
"node"
]
}
}
172 changes: 172 additions & 0 deletions packages/altair-koa-middleware/yarn.lock
@@ -0,0 +1,172 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


altair-static@^2.1.5:
version "2.1.5"
resolved "https://registry.yarnpkg.com/altair-static/-/altair-static-2.1.5.tgz#d907475e61414de401eccb81b0df423b05d24d2d"
integrity sha512-NpuB/4Wu0FnsNnpvzsPj7nkGsQ586IPXPmaUA13FVj+9Ea7UiTynL2LC4M3NkFsdiIyTBVXUHIphR1ECdUWgZQ==

any-promise@^1.0.0, any-promise@^1.1.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f"
integrity sha1-q8av7tzqUugJzcA3au0845Y10X8=

debug@^3.1.0:
version "3.2.6"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b"
integrity sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==
dependencies:
ms "^2.1.1"

depd@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
integrity sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=

http-errors@^1.3.1, http-errors@^1.6.3:
version "1.7.2"
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.7.2.tgz#4f5029cf13239f31036e5b2e55292bcfbcc85c8f"
integrity sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==
dependencies:
depd "~1.1.2"
inherits "2.0.3"
setprototypeof "1.1.1"
statuses ">= 1.5.0 < 2"
toidentifier "1.0.0"

http-errors@~1.6.2:
version "1.6.3"
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.6.3.tgz#8b55680bb4be283a0b5bf4ea2e38580be1d9320d"
integrity sha1-i1VoC7S+KDoLW/TqLjhYC+HZMg0=
dependencies:
depd "~1.1.2"
inherits "2.0.3"
setprototypeof "1.1.0"
statuses ">= 1.4.0 < 2"

inherits@2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=

isarray@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
integrity sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=

koa-compose@^3.0.0:
version "3.2.1"
resolved "https://registry.yarnpkg.com/koa-compose/-/koa-compose-3.2.1.tgz#a85ccb40b7d986d8e5a345b3a1ace8eabcf54de7"
integrity sha1-qFzLQLfZhtjlo0Wzoazo6rz1Tec=
dependencies:
any-promise "^1.1.0"

koa-router@^7.4.0:
version "7.4.0"
resolved "https://registry.yarnpkg.com/koa-router/-/koa-router-7.4.0.tgz#aee1f7adc02d5cb31d7d67465c9eacc825e8c5e0"
integrity sha512-IWhaDXeAnfDBEpWS6hkGdZ1ablgr6Q6pGdXCyK38RbzuH4LkUOpPqPw+3f8l8aTDrQmBQ7xJc0bs2yV4dzcO+g==
dependencies:
debug "^3.1.0"
http-errors "^1.3.1"
koa-compose "^3.0.0"
methods "^1.0.1"
path-to-regexp "^1.1.1"
urijs "^1.19.0"

koa-send@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/koa-send/-/koa-send-5.0.0.tgz#5e8441e07ef55737734d7ced25b842e50646e7eb"
integrity sha512-90ZotV7t0p3uN9sRwW2D484rAaKIsD8tAVtypw/aBU+ryfV+fR2xrcAwhI8Wl6WRkojLUs/cB9SBSCuIb+IanQ==
dependencies:
debug "^3.1.0"
http-errors "^1.6.3"
mz "^2.7.0"
resolve-path "^1.4.0"

methods@^1.0.1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
integrity sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=

ms@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a"
integrity sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==

mz@^2.7.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/mz/-/mz-2.7.0.tgz#95008057a56cafadc2bc63dde7f9ff6955948e32"
integrity sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q==
dependencies:
any-promise "^1.0.0"
object-assign "^4.0.1"
thenify-all "^1.0.0"

object-assign@^4.0.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=

path-is-absolute@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18=

path-to-regexp@^1.1.1:
version "1.7.0"
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-1.7.0.tgz#59fde0f435badacba103a84e9d3bc64e96b9937d"
integrity sha1-Wf3g9DW62suhA6hOnTvGTpa5k30=
dependencies:
isarray "0.0.1"

resolve-path@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/resolve-path/-/resolve-path-1.4.0.tgz#c4bda9f5efb2fce65247873ab36bb4d834fe16f7"
integrity sha1-xL2p9e+y/OZSR4c6s2u02DT+Fvc=
dependencies:
http-errors "~1.6.2"
path-is-absolute "1.0.1"

setprototypeof@1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.1.0.tgz#d0bd85536887b6fe7c0d818cb962d9d91c54e656"
integrity sha512-BvE/TwpZX4FXExxOxZyRGQQv651MSwmWKZGqvmPcRIjDqWub67kTKuIMx43cZZrS/cBBzwBcNDWoFxt2XEFIpQ==

setprototypeof@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.1.1.tgz#7e95acb24aa92f5885e0abef5ba131330d4ae683"
integrity sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==

"statuses@>= 1.4.0 < 2", "statuses@>= 1.5.0 < 2":
version "1.5.0"
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
integrity sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=

thenify-all@^1.0.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/thenify-all/-/thenify-all-1.6.0.tgz#1a1918d402d8fc3f98fbf234db0bcc8cc10e9726"
integrity sha1-GhkY1ALY/D+Y+/I02wvMjMEOlyY=
dependencies:
thenify ">= 3.1.0 < 4"

"thenify@>= 3.1.0 < 4":
version "3.3.0"
resolved "https://registry.yarnpkg.com/thenify/-/thenify-3.3.0.tgz#e69e38a1babe969b0108207978b9f62b88604839"
integrity sha1-5p44obq+lpsBCCB5eLn2K4hgSDk=
dependencies:
any-promise "^1.0.0"

toidentifier@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553"
integrity sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==

typescript@^2.8.1:
version "2.8.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.8.1.tgz#6160e4f8f195d5ba81d4876f9c0cc1fbc0820624"

urijs@^1.19.0:
version "1.19.1"
resolved "https://registry.yarnpkg.com/urijs/-/urijs-1.19.1.tgz#5b0ff530c0cbde8386f6342235ba5ca6e995d25a"
integrity sha512-xVrGVi94ueCJNrBSTjWqjvtgvl3cyOTThp2zaMaFNGp3F542TR6sM3f2o8RqZl+AwteClSVmoCyt0ka4RjQOQg==

0 comments on commit 9350936

Please sign in to comment.