@vroskus/library-social 1.0.0 → 1.0.1

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/.eslintrc ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "root": true,
3
+ "parser": "@typescript-eslint/parser",
4
+ "parserOptions": {
5
+ "requireConfigFile": false,
6
+ "project": ["tsconfig.eslint.json"]
7
+ },
8
+ "extends": [
9
+ "airbnb-base",
10
+ "airbnb-typescript",
11
+ "plugin:import/recommended",
12
+ "plugin:@typescript-eslint/eslint-recommended",
13
+ "plugin:@typescript-eslint/recommended",
14
+ "plugin:sort/recommended"
15
+ ],
16
+ "plugins": [
17
+ "@typescript-eslint",
18
+ "import",
19
+ "import-newlines",
20
+ "react",
21
+ "sort"
22
+ ],
23
+ "rules": {
24
+ "complexity": ["error", 5],
25
+ "@typescript-eslint/ban-ts-comment": [1],
26
+ "@typescript-eslint/no-unused-vars": [2],
27
+ "curly": ["error", "all"],
28
+ "import/no-cycle": [0],
29
+ "no-await-in-loop": 0,
30
+ "no-console": "error",
31
+ "no-duplicate-imports": [0],
32
+ "no-loss-of-precision": [0],
33
+ "no-restricted-syntax": 0,
34
+ "no-unreachable-loop": [0],
35
+ "no-useless-backreference": [0],
36
+ "no-unused-private-class-members": [2],
37
+ "padding-line-between-statements": ["error", {
38
+ "blankLine": "always",
39
+ "prev": ["const", "let"],
40
+ "next": "*"
41
+ }, {
42
+ "blankLine": "any",
43
+ "prev": ["const", "let"],
44
+ "next": ["const", "let"]
45
+ }, {
46
+ "blankLine": "always",
47
+ "prev": ["if", "function", "for"],
48
+ "next": "*"
49
+ }, {
50
+ "blankLine": "always",
51
+ "prev": "*",
52
+ "next": "return"
53
+ }],
54
+ "object-curly-newline": ["error", {
55
+ "ObjectExpression": "always",
56
+ "ObjectPattern": "always",
57
+ "ImportDeclaration": "always",
58
+ "ExportDeclaration": "always"
59
+ }],
60
+ "function-call-argument-newline": ["error", "always"],
61
+ "object-property-newline": ["error", {
62
+ "allowMultiplePropertiesPerLine": false
63
+ }],
64
+ "import-newlines/enforce": ["error", {
65
+ "items": 1,
66
+ "forceSingleLine": false
67
+ }],
68
+ "sort/imports": [0],
69
+ "sort/exports": [0]
70
+ },
71
+ "env": {
72
+ "browser": true,
73
+ "node": true,
74
+ "jest": true,
75
+ "es6": true
76
+ }
77
+ }
@@ -0,0 +1,15 @@
1
+ import type { Axios } from 'axios';
2
+ import type { $Config as Config, $FacebookPayload, $GooglePayload } from './types';
3
+ export type $Config = Config;
4
+ declare class Social<C extends Config> {
5
+ #private;
6
+ connection: Axios;
7
+ constructor({ mock, timeout, }: C);
8
+ authenticateFacebook(params: {
9
+ token: string;
10
+ }): Promise<$FacebookPayload>;
11
+ authenticateGoogle(params: {
12
+ token: string;
13
+ }): Promise<$GooglePayload>;
14
+ }
15
+ export default Social;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAMA,kDAA0B;AAC1B,4EAAkD;AAGlD,mCAGiB;AAWjB,MAAM,MAAM;IAGV,YAAY,EACV,IAAI,EACJ,OAAO,GACL;;QACF,IAAI,IAAI,KAAK,IAAI,EAAE;YACjB,uBAAA,IAAI,4CAAW,MAAf,IAAI,CAAa,CAAC;SACnB;QAGD,IAAI,CAAC,UAAU,GAAG,eAAK,CAAC,MAAM,CAAC;YAC7B,OAAO,EAAE;gBACP,MAAM,EAAE,kBAAkB;gBAC1B,cAAc,EAAE,kBAAkB;aACnC;YACD,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IAmBK,oBAAoB,CAAC,MAE1B;;YACC,MAAM,GAAG,GAAG,oCAAoC,CAAC;YACjD,MAAM,OAAO,GAAG;gBACd,MAAM,EAAE;oBACN,YAAY,EAAE,MAAM,CAAC,KAAK;oBAC1B,MAAM,EAAE,oBAAoB;iBAC7B;aACF,CAAC;YACF,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,GAAG,CACxC,GAAG,EACH,OAAO,CACR,CAAC;YAEF,OAAO,QAAQ,CAAC,IAAI,CAAC;QACvB,CAAC;KAAA;IAEK,kBAAkB,CAAC,MAExB;;YACC,MAAM,GAAG,GAAG,+CAA+C,CAAC;YAC5D,MAAM,OAAO,GAAG;gBACd,MAAM,EAAE;oBACN,YAAY,EAAE,MAAM,CAAC,KAAK;iBAC3B;aACF,CAAC;YACF,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,GAAG,CACxC,GAAG,EACH,OAAO,CACR,CAAC;YAEF,OAAO,QAAQ,CAAC,IAAI,CAAC;QACvB,CAAC;KAAA;CACF;;IAjDG,MAAM,IAAI,GAAG,IAAI,4BAAgB,CAAC,eAAK,CAAC,CAAC;IAEzC,IAAI,CAAC,KAAK,CAAC,oCAAoC,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE;QAC1D,MAAM,YAAY,GAAqB,oBAAY,CAAC;QAEpD,OAAO,CAAC,GAAG,EAAE,YAAY,CAAC,CAAC;IAC7B,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,KAAK,CAAC,oDAAoD,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE;QAC1E,MAAM,YAAY,GAAmB,kBAAU,CAAC;QAEhD,OAAO,CAAC,GAAG,EAAE,YAAY,CAAC,CAAC;IAC7B,CAAC,CAAC,CAAC;AACL,CAAC;AAsCH,kBAAe,MAAM,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { $FacebookPayload, $GooglePayload } from './types';
2
+ export declare const facebookMock: $FacebookPayload;
3
+ export declare const googleMock: $GooglePayload;
package/dist/mocks.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.googleMock = exports.facebookMock = void 0;
4
+ exports.facebookMock = {
5
+ email: 'user@facebook.com',
6
+ id: '123',
7
+ name: 'facebook-user',
8
+ };
9
+ exports.googleMock = {
10
+ email: 'user@google.com',
11
+ family_name: 'Lastname',
12
+ given_name: 'Firstname',
13
+ hd: 'hd',
14
+ id: '123',
15
+ locale: 'en',
16
+ name: 'google-user',
17
+ picture: 'http://picture.url',
18
+ verified_email: true,
19
+ };
20
+ //# sourceMappingURL=mocks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mocks.js","sourceRoot":"","sources":["../src/mocks.ts"],"names":[],"mappings":";;;AAMa,QAAA,YAAY,GAAqB;IAC5C,KAAK,EAAE,mBAAmB;IAC1B,EAAE,EAAE,KAAK;IACT,IAAI,EAAE,eAAe;CACtB,CAAC;AAEW,QAAA,UAAU,GAAmB;IACxC,KAAK,EAAE,iBAAiB;IACxB,WAAW,EAAE,UAAU;IACvB,UAAU,EAAE,WAAW;IACvB,EAAE,EAAE,IAAI;IACR,EAAE,EAAE,KAAK;IACT,MAAM,EAAE,IAAI;IACZ,IAAI,EAAE,aAAa;IACnB,OAAO,EAAE,oBAAoB;IAC7B,cAAc,EAAE,IAAI;CACrB,CAAC"}
@@ -0,0 +1,25 @@
1
+ export type $Config = {
2
+ mock?: boolean;
3
+ timeout: number;
4
+ };
5
+ export type $FacebookPayload = {
6
+ email: string;
7
+ id: string;
8
+ name: string;
9
+ picture?: {
10
+ data: {
11
+ url: string;
12
+ };
13
+ };
14
+ };
15
+ export type $GooglePayload = {
16
+ email: string;
17
+ family_name: string;
18
+ given_name: string;
19
+ hd: string;
20
+ id: string;
21
+ locale: string;
22
+ name: string;
23
+ picture: string;
24
+ verified_email: boolean;
25
+ };
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vroskus/library-social",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Social",
5
5
  "author": "Vilius Roškus <vilius@regattas.eu>",
6
6
  "license": "MIT",
@@ -9,9 +9,6 @@
9
9
  "url": "git+https://github.com/vroskus/library-social.git"
10
10
  },
11
11
  "main": "dist/index.js",
12
- "files": [
13
- "dist/index.js"
14
- ],
15
12
  "scripts": {
16
13
  "postinstall": "npm run build",
17
14
  "build": "tsc",
@@ -22,6 +19,7 @@
22
19
  "test:e2e:post": "jest-coverage-thresholds-bumper"
23
20
  },
24
21
  "dependencies": {
22
+ "@types/axios": "0.14.0",
25
23
  "axios": "1.3.4",
26
24
  "axios-mock-adapter": "1.21.2"
27
25
  },
package/src/index.ts ADDED
@@ -0,0 +1,99 @@
1
+ // Global Types
2
+ import type {
3
+ Axios,
4
+ } from 'axios';
5
+
6
+ // Helpers
7
+ import axios from 'axios';
8
+ import AxiosMockAdapter from 'axios-mock-adapter';
9
+
10
+ // Mocks
11
+ import {
12
+ facebookMock,
13
+ googleMock,
14
+ } from './mocks';
15
+
16
+ // Types
17
+ import type {
18
+ $Config as Config,
19
+ $FacebookPayload,
20
+ $GooglePayload,
21
+ } from './types';
22
+
23
+ export type $Config = Config;
24
+
25
+ class Social<C extends Config> {
26
+ connection: Axios;
27
+
28
+ constructor({
29
+ mock,
30
+ timeout,
31
+ }: C) {
32
+ if (mock === true) {
33
+ this.#setupMock();
34
+ }
35
+
36
+ // Connection setup
37
+ this.connection = axios.create({
38
+ headers: {
39
+ Accept: 'application/json',
40
+ 'Content-Type': 'application/json',
41
+ },
42
+ timeout,
43
+ });
44
+ }
45
+
46
+ // eslint-disable-next-line
47
+ #setupMock(): void {
48
+ const mock = new AxiosMockAdapter(axios);
49
+
50
+ mock.onGet('https://graph.facebook.com/v9.0/me').reply(() => {
51
+ const responseBody: $FacebookPayload = facebookMock;
52
+
53
+ return [200, responseBody];
54
+ });
55
+
56
+ mock.onGet(/https:\/\/www.googleapis.com\/oauth2\/v2\/userinfo/).reply(() => {
57
+ const responseBody: $GooglePayload = googleMock;
58
+
59
+ return [200, responseBody];
60
+ });
61
+ }
62
+
63
+ async authenticateFacebook(params: {
64
+ token: string;
65
+ }): Promise<$FacebookPayload> {
66
+ const url = 'https://graph.facebook.com/v9.0/me';
67
+ const request = {
68
+ params: {
69
+ access_token: params.token,
70
+ fields: 'name,email,picture',
71
+ },
72
+ };
73
+ const response = await this.connection.get(
74
+ url,
75
+ request,
76
+ );
77
+
78
+ return response.data;
79
+ }
80
+
81
+ async authenticateGoogle(params: {
82
+ token: string;
83
+ }): Promise<$GooglePayload> {
84
+ const url = 'https://www.googleapis.com/oauth2/v2/userinfo';
85
+ const request = {
86
+ params: {
87
+ access_token: params.token,
88
+ },
89
+ };
90
+ const response = await this.connection.get(
91
+ url,
92
+ request,
93
+ );
94
+
95
+ return response.data;
96
+ }
97
+ }
98
+
99
+ export default Social;
package/src/mocks.ts ADDED
@@ -0,0 +1,23 @@
1
+ // Types
2
+ import type {
3
+ $FacebookPayload,
4
+ $GooglePayload,
5
+ } from './types';
6
+
7
+ export const facebookMock: $FacebookPayload = {
8
+ email: 'user@facebook.com',
9
+ id: '123',
10
+ name: 'facebook-user',
11
+ };
12
+
13
+ export const googleMock: $GooglePayload = {
14
+ email: 'user@google.com',
15
+ family_name: 'Lastname',
16
+ given_name: 'Firstname',
17
+ hd: 'hd',
18
+ id: '123',
19
+ locale: 'en',
20
+ name: 'google-user',
21
+ picture: 'http://picture.url',
22
+ verified_email: true,
23
+ };
package/src/types.ts ADDED
@@ -0,0 +1,27 @@
1
+ export type $Config = {
2
+ mock?: boolean;
3
+ timeout: number;
4
+ };
5
+
6
+ export type $FacebookPayload = {
7
+ email: string;
8
+ id: string;
9
+ name: string;
10
+ picture?: {
11
+ data: {
12
+ url: string;
13
+ };
14
+ };
15
+ };
16
+
17
+ export type $GooglePayload = {
18
+ email: string;
19
+ family_name: string;
20
+ given_name: string;
21
+ hd: string;
22
+ id: string;
23
+ locale: string;
24
+ name: string;
25
+ picture: string;
26
+ verified_email: boolean;
27
+ };
@@ -0,0 +1,40 @@
1
+ import Social from '../src';
2
+
3
+ import {
4
+ facebookMock,
5
+ googleMock,
6
+ } from '../src/mocks';
7
+
8
+ describe(
9
+ 'Social',
10
+ () => {
11
+ const service = new Social({
12
+ mock: true,
13
+ timeout: 0,
14
+ });
15
+
16
+ it(
17
+ 'shoud get FACEBOOK auth data',
18
+ async () => {
19
+ const params = {
20
+ token: 'random-string',
21
+ };
22
+ const data = await service.authenticateFacebook(params);
23
+
24
+ expect(data).toMatchObject(facebookMock);
25
+ },
26
+ );
27
+
28
+ it(
29
+ 'shoud get GOOGLE auth data',
30
+ async () => {
31
+ const params = {
32
+ token: 'random-string',
33
+ };
34
+ const data = await service.authenticateGoogle(params);
35
+
36
+ expect(data).toMatchObject(googleMock);
37
+ },
38
+ );
39
+ },
40
+ );
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "include": ["src", "test"]
4
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "./dist/",
4
+ "types": ["node", "jest"],
5
+ "module": "commonjs",
6
+ "esModuleInterop": true,
7
+ "declaration": true,
8
+ "noImplicitAny": false,
9
+ "removeComments": true,
10
+ "noLib": false,
11
+ "emitDecoratorMetadata": true,
12
+ "experimentalDecorators": true,
13
+ "target": "es6",
14
+ "sourceMap": true,
15
+ "lib": [
16
+ "dom",
17
+ "es6"
18
+ ]
19
+ },
20
+ "exclude": [
21
+ "node_modules",
22
+ "./dist/**/*"
23
+ ],
24
+ "include": [
25
+ "./src"
26
+ ]
27
+ }