@hellocoop/fastify 1.12.3 → 1.13.0

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Hellō
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/auth.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { FastifyPluginAsync } from 'fastify';
2
2
  import { FastifyPluginOptions } from 'fastify';
3
- import { Auth } from '@hellocoop/types';
4
- import { Config } from '@hellocoop/router';
3
+ import { Auth } from '@hellocoop/definitions';
4
+ import { Config } from '@hellocoop/api';
5
5
  declare module 'fastify' {
6
6
  interface FastifyRequest {
7
7
  auth?: Auth;
@@ -1 +1 @@
1
- {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAEA,OAAO,EACH,kBAAkB,EACpB,MAAM,SAAS,CAAA;AAGjB,OAAO,EAGH,oBAAoB,EACvB,MAAM,SAAS,CAAA;AAIhB,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AACvC,OAAO,EASH,MAAM,EACT,MAAO,mBAAmB,CAAA;AAwC3B,OAAO,QAAQ,SAAS,CAAC;IACrB,UAAU,cAAc;QACtB,IAAI,CAAC,EAAE,IAAI,CAAC;QACZ,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;KAC7B;IACD,UAAU,YAAY;QACpB,SAAS,EAAE,MAAM,IAAI,CAAC;KACvB;CACF;AAEH,MAAM,WAAW,WAAY,SAAQ,oBAAoB,EAAE,MAAM;CAAG;AA6BpE,eAAO,MAAM,IAAI,iCAAoB,CAAA"}
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAEA,OAAO,EACH,kBAAkB,EACpB,MAAM,SAAS,CAAA;AAGjB,OAAO,EAGH,oBAAoB,EACvB,MAAM,SAAS,CAAA;AAIhB,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAA;AAC7C,OAAO,EASH,MAAM,EACT,MAAO,gBAAgB,CAAA;AAgDxB,OAAO,QAAQ,SAAS,CAAC;IACrB,UAAU,cAAc;QACtB,IAAI,CAAC,EAAE,IAAI,CAAC;QACZ,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;KAC7B;IACD,UAAU,YAAY;QACpB,SAAS,EAAE,MAAM,IAAI,CAAC;KACvB;CACF;AAEH,MAAM,WAAW,WAAY,SAAQ,oBAAoB,EAAE,MAAM;CAAG;AA8BpE,eAAO,MAAM,IAAI,iCAAoB,CAAA"}
package/dist/auth.js CHANGED
@@ -7,22 +7,29 @@ Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.auth = void 0;
8
8
  const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
9
9
  const cookie_1 = require("cookie");
10
- const router_1 = require("@hellocoop/router");
11
- const convertToHelloRequest = (req) => {
10
+ const api_1 = require("@hellocoop/api");
11
+ const convertToHelloRequest = (req, res) => {
12
12
  return {
13
13
  headers: () => { return req.headers; },
14
14
  query: req.query,
15
- path: req.routeOptions.url,
15
+ path: req?.routeOptions?.url || '',
16
16
  getAuth: () => { return req.auth; },
17
17
  setAuth: (auth) => { req.auth = auth; },
18
18
  method: req.method,
19
- body: req.body
19
+ body: req.body,
20
+ frameWork: 'fastify',
21
+ loginSyncWrapper: (loginSync, params) => {
22
+ return loginSync({ ...params, req, res });
23
+ },
24
+ logoutSyncWrapper: (logoutSync) => {
25
+ return logoutSync({ req, res });
26
+ },
20
27
  };
21
28
  };
22
29
  const convertToHelloResponse = (res) => {
23
30
  return {
24
31
  clearAuth: () => {
25
- const { name, value, options } = (0, router_1.clearAuthCookieParams)();
32
+ const { name, value, options } = (0, api_1.clearAuthCookieParams)();
26
33
  res.header('Set-Cookie', (0, cookie_1.serialize)(name, value, options));
27
34
  },
28
35
  send: (data) => res.type('text/html').send(data),
@@ -42,27 +49,28 @@ const convertToHelloResponse = (res) => {
42
49
  };
43
50
  };
44
51
  const helloPlugin = async (instance, options) => {
45
- if (!router_1.isConfigured)
46
- (0, router_1.configure)(options);
52
+ if (!api_1.isConfigured)
53
+ (0, api_1.configure)(options);
47
54
  instance.decorateRequest('auth', undefined);
48
55
  instance.decorateRequest('getAuth', async function () {
49
- const helloReq = convertToHelloRequest(this);
50
- this.auth = await (0, router_1.getAuthfromCookies)(helloReq);
56
+ const dummyResponse = {};
57
+ const helloReq = convertToHelloRequest(this, dummyResponse);
58
+ this.auth = await (0, api_1.getAuthfromCookies)(helloReq);
51
59
  return this.auth;
52
60
  });
53
61
  instance.decorateReply('clearAuth', function () {
54
- const { name, value, options } = (0, router_1.clearAuthCookieParams)();
62
+ const { name, value, options } = (0, api_1.clearAuthCookieParams)();
55
63
  this.header('Set-Cookie', (0, cookie_1.serialize)(name, value, options));
56
64
  });
57
- instance.get(router_1.configuration.apiRoute, async (req, res) => {
58
- const helloReq = convertToHelloRequest(req);
65
+ instance.get(api_1.configuration.apiRoute, async (req, res) => {
66
+ const helloReq = convertToHelloRequest(req, res);
59
67
  const helloRes = convertToHelloResponse(res);
60
- return await (0, router_1.router)(helloReq, helloRes);
68
+ return await (0, api_1.router)(helloReq, helloRes);
61
69
  });
62
- instance.post(router_1.configuration.apiRoute, async (req, res) => {
63
- const helloReq = convertToHelloRequest(req);
70
+ instance.post(api_1.configuration.apiRoute, async (req, res) => {
71
+ const helloReq = convertToHelloRequest(req, res);
64
72
  const helloRes = convertToHelloResponse(res);
65
- return await (0, router_1.router)(helloReq, helloRes);
73
+ return await (0, api_1.router)(helloReq, helloRes);
66
74
  });
67
75
  };
68
76
  exports.auth = (0, fastify_plugin_1.default)(helloPlugin);
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { auth, HelloConfig } from './auth';
2
2
  export default auth;
3
3
  export { auth as helloAuth, HelloConfig };
4
- export { LoginSyncParams, LoginSyncResponse, LogoutSyncParams, LogoutSyncResponse } from '@hellocoop/router';
4
+ export { LoginSyncResponse, LogoutSyncResponse } from '@hellocoop/api';
5
5
  export { redirect, unauthorized, setAuth } from './middleware';
6
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAA;AAC1C,eAAe,IAAI,CAAA;AACnB,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,WAAW,EAAE,CAAA;AACzC,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAC5G,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAA;AAC1C,eAAe,IAAI,CAAA;AACnB,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,WAAW,EAAE,CAAA;AACzC,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AACtE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA"}
@@ -1,5 +1,5 @@
1
1
  import { FastifyRequest, FastifyReply } from 'fastify';
2
- import { Auth } from '@hellocoop/types';
2
+ import { Auth } from '@hellocoop/definitions';
3
3
  declare module 'fastify' {
4
4
  interface FastifyRequest {
5
5
  getAuth: () => Promise<Auth>;
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACtD,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AAGvC,OAAO,QAAQ,SAAS,CAAC;IACvB,UAAU,cAAc;QACtB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;KAC9B;CACF;AAED,eAAO,MAAM,QAAQ,WAAY,MAAM,eACd,cAAc,SAAS,YAAY,uBAQ3D,CAAC;AAEF,eAAO,MAAM,YAAY,YAAmB,cAAc,SAAS,YAAY,uBAM9E,CAAC;AAEF,eAAO,MAAM,OAAO,YAAmB,cAAc,SAAS,YAAY,kBAEzE,CAAC"}
1
+ {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACtD,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAA;AAG7C,OAAO,QAAQ,SAAS,CAAC;IACvB,UAAU,cAAc;QACtB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;KAC9B;CACF;AAED,eAAO,MAAM,QAAQ,WAAY,MAAM,eACd,cAAc,SAAS,YAAY,uBAQ3D,CAAC;AAEF,eAAO,MAAM,YAAY,YAAmB,cAAc,SAAS,YAAY,uBAM9E,CAAC;AAEF,eAAO,MAAM,OAAO,YAAmB,cAAc,SAAS,YAAY,kBAEzE,CAAC"}
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.setAuth = exports.unauthorized = exports.redirect = void 0;
4
- const router_1 = require("@hellocoop/router");
4
+ const api_1 = require("@hellocoop/api");
5
5
  const redirect = (target) => {
6
6
  return async (request, reply) => {
7
7
  const auth = await request.getAuth();
@@ -17,7 +17,7 @@ exports.redirect = redirect;
17
17
  const unauthorized = async (request, reply) => {
18
18
  const auth = await request.getAuth();
19
19
  if (!auth.isLoggedIn) {
20
- reply.header('WWW-Authenticate', `Hello ${router_1.configuration.clientId}`).status(401);
20
+ reply.header('WWW-Authenticate', `Hello ${api_1.configuration.clientId}`).status(401);
21
21
  return reply.send();
22
22
  }
23
23
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hellocoop/fastify",
3
- "version": "1.12.3",
3
+ "version": "1.13.0",
4
4
  "description": "Fastify SDK for Hellō https://hello.dev",
5
5
  "repository": {
6
6
  "type": "git",
@@ -35,9 +35,10 @@
35
35
  },
36
36
  "scripts": {
37
37
  "watch": "tsc --watch --declaration",
38
- "build": "npm run build:clean && npm run build:src",
39
- "build:clean": "rimraf dist/",
40
- "build:src": "tsc --declaration"
38
+ "prebuild": "rimraf dist node_modules",
39
+ "build": "tsc --declaration",
40
+ "test": "npm run build && docker compose -f ./spec/docker-compose.yml up --build --abort-on-container-exit",
41
+ "spec": "wait-on http://mockin:3333/mock && mocha './spec/**/*.spec.mjs'"
41
42
  },
42
43
  "peerDependencies": {
43
44
  "fastify": "^4"
@@ -52,8 +53,9 @@
52
53
  "node": ">=18"
53
54
  },
54
55
  "dependencies": {
55
- "@hellocoop/router": "~1.11.1",
56
+ "@hellocoop/api": "~2.0.0",
56
57
  "dotenv": "^16.3.1",
57
58
  "fastify-plugin": "^4.5.1"
58
- }
59
+ },
60
+ "gitHead": "018e57826706471620d6ac09e66121e53877c87e"
59
61
  }
package/dist/server.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=server.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":""}
package/dist/server.js DELETED
@@ -1,73 +0,0 @@
1
- "use strict";
2
- // test server
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- // import fastly from 'fastify'
8
- // import auth from './index'
9
- // import { redirect, unauthorized, setAuth } from './middleware'
10
- // const app = fastly();
11
- // const port = 8080; // default port to listen
12
- // app.register(auth,{client_id:'90804992-8d01-474e-8a0c-59cddeb5a1a3'})
13
- // app.get( "/", async ( req, res ) => {
14
- // res.send( await req.getAuth())
15
- // } );
16
- // app.get( "/redirect", redirect('/'), async ( req, res ) => {
17
- // res.send( await req.getAuth())
18
- // } );
19
- // app.get( "/unauthorized", unauthorized, async ( req, res ) => {
20
- // res.send( await req.getAuth())
21
- // } );
22
- // app.get( "/setAuth", setAuth, ( req, res ) => {
23
- // res.send( req.auth)
24
- // } );
25
- // // start the Express server
26
- // app.listen( port, () => {
27
- // console.log( `server started at http://localhost:${ port }` );
28
- // } );
29
- const fastify_1 = __importDefault(require("fastify"));
30
- const index_1 = require("./index");
31
- const middleware_1 = require("./middleware");
32
- const config = require('../hello.config.js');
33
- const app = (0, fastify_1.default)();
34
- const port = 8080;
35
- // Register your auth plugin with Fastify
36
- app.register(index_1.helloAuth, config);
37
- // Define your routes, using preHandler hooks to replace Express middleware
38
- app.get("/", async (request, reply) => {
39
- const auth = await request.getAuth();
40
- return reply.send(auth);
41
- });
42
- app.get("/redirect", {
43
- preHandler: (0, middleware_1.redirect)('/'),
44
- handler: async (request, reply) => {
45
- const auth = await request.getAuth();
46
- return reply.send(auth);
47
- }
48
- });
49
- app.get("/unauthorized", {
50
- preHandler: middleware_1.unauthorized,
51
- handler: async (request, reply) => {
52
- const auth = await request.getAuth();
53
- return reply.send(auth);
54
- }
55
- });
56
- app.get("/setAuth", {
57
- preHandler: middleware_1.setAuth,
58
- handler: async (request, reply) => {
59
- // Assuming setAuth sets something like request.auth
60
- return reply.send(request.auth);
61
- }
62
- });
63
- const startServer = async () => {
64
- try {
65
- await app.listen({ port: port });
66
- console.log(`Server started at http://localhost:${port}`);
67
- }
68
- catch (err) {
69
- console.error(err);
70
- process.exit(1);
71
- }
72
- };
73
- startServer();