@smartsoft001/google 1.1.90 → 1.2.0
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.json +26 -0
- package/README.md +21 -4
- package/jest.config.ts +27 -0
- package/package.json +3 -12
- package/project.json +37 -0
- package/src/index.ts +1 -0
- package/src/lib/google.service.spec.ts +100 -0
- package/src/lib/google.service.ts +30 -0
- package/tsconfig.json +13 -0
- package/tsconfig.lib.json +11 -0
- package/tsconfig.spec.json +20 -0
- package/src/index.d.ts +0 -1
- package/src/index.js +0 -5
- package/src/index.js.map +0 -1
- package/src/lib/google.service.d.ts +0 -10
- package/src/lib/google.service.js +0 -29
- package/src/lib/google.service.js.map +0 -1
- package/test-setup.js +0 -1
- package/test-setup.js.map +0 -1
- /package/{test-setup.d.ts → test-setup.ts} +0 -0
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": ["../../../.eslintrc.json"],
|
|
3
|
+
"ignorePatterns": ["!**/*"],
|
|
4
|
+
"rules": {
|
|
5
|
+
"import/order": "off"
|
|
6
|
+
},
|
|
7
|
+
"overrides": [
|
|
8
|
+
{
|
|
9
|
+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
|
|
10
|
+
"rules": {}
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"files": ["*.ts", "*.tsx"],
|
|
14
|
+
"rules": {}
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"files": ["*.js", "*.jsx"],
|
|
18
|
+
"rules": {}
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"files": "package.json",
|
|
22
|
+
"parser": "jsonc-eslint-parser",
|
|
23
|
+
"rules": {}
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
}
|
package/README.md
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 📦 @smartsoft001/google
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
 
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 🚀 Usage
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
`npm i @smartsoft001/google`
|
|
8
|
+
|
|
9
|
+
## 🛠️ Services & Methods
|
|
10
|
+
|
|
11
|
+
### GoogleService
|
|
12
|
+
|
|
13
|
+
Methods:
|
|
14
|
+
|
|
15
|
+
<table>
|
|
16
|
+
<tr>
|
|
17
|
+
<td>getUserId</td>
|
|
18
|
+
<td>Makes a GET request to Google's OAuth tokeninfo endpoint to validate the token and get user information</td>
|
|
19
|
+
</tr>
|
|
20
|
+
<tr>
|
|
21
|
+
<td>getData</td>
|
|
22
|
+
<td>Makes the same GET request to the tokeninfo endpoint, but processes the response data to return an object containing id and email</td>
|
|
23
|
+
</tr>
|
|
24
|
+
</table>
|
package/jest.config.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
export default {
|
|
3
|
+
globals: {},
|
|
4
|
+
transform: {
|
|
5
|
+
'^.+\\.[tj]sx?$': [
|
|
6
|
+
'ts-jest',
|
|
7
|
+
{
|
|
8
|
+
tsConfig: '<rootDir>/tsconfig.spec.json',
|
|
9
|
+
},
|
|
10
|
+
],
|
|
11
|
+
},
|
|
12
|
+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
|
|
13
|
+
coverageDirectory: '../../../coverage/packages/shared/google',
|
|
14
|
+
displayName: 'shared-google',
|
|
15
|
+
testEnvironment: 'node',
|
|
16
|
+
preset: '../../../jest.preset.js',
|
|
17
|
+
/* TODO: Update to latest Jest snapshotFormat
|
|
18
|
+
* By default Nx has kept the older style of Jest Snapshot formats
|
|
19
|
+
* to prevent breaking of any existing tests with snapshots.
|
|
20
|
+
* It's recommend you update to the latest format.
|
|
21
|
+
* You can do this by removing snapshotFormat property
|
|
22
|
+
* and running tests with --update-snapshot flag.
|
|
23
|
+
* Example: From within the project directory, run "nx test --update-snapshot"
|
|
24
|
+
* More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format
|
|
25
|
+
*/
|
|
26
|
+
snapshotFormat: { escapeString: true, printBasicPrototype: true },
|
|
27
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smartsoft001/google",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "Utils for google authorisation",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/emiljuchnikowski/smartsoft.git"
|
|
@@ -11,14 +11,5 @@
|
|
|
11
11
|
"bugs": {
|
|
12
12
|
"url": "https://github.com/emiljuchnikowski/smartsoft/issues"
|
|
13
13
|
},
|
|
14
|
-
"homepage": "https://github.com/emiljuchnikowski/smartsoft#readme"
|
|
15
|
-
"dependencies": {
|
|
16
|
-
"@nestjs/axios": "3.0.0",
|
|
17
|
-
"@nestjs/common": "^10.0.5"
|
|
18
|
-
},
|
|
19
|
-
"peerDependencies": {
|
|
20
|
-
"tslib": "2.5.3"
|
|
21
|
-
},
|
|
22
|
-
"main": "./src/index.js",
|
|
23
|
-
"types": "./src/index.d.ts"
|
|
14
|
+
"homepage": "https://github.com/emiljuchnikowski/smartsoft#readme"
|
|
24
15
|
}
|
package/project.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "shared-google",
|
|
3
|
+
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"sourceRoot": "packages/shared/google/src",
|
|
5
|
+
"projectType": "library",
|
|
6
|
+
"targets": {
|
|
7
|
+
"lint": {
|
|
8
|
+
"executor": "@nx/eslint:lint",
|
|
9
|
+
"outputs": ["{options.outputFile}"],
|
|
10
|
+
"options": {
|
|
11
|
+
"lintFilePatterns": [
|
|
12
|
+
"packages/shared/fb/**/*.{ts,tsx,js,jsx}",
|
|
13
|
+
"packages/shared/fb/package.json"
|
|
14
|
+
]
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"test": {
|
|
18
|
+
"executor": "@nx/jest:jest",
|
|
19
|
+
"outputs": ["{workspaceRoot}/coverage/packages/shared/google"],
|
|
20
|
+
"options": {
|
|
21
|
+
"jestConfig": "packages/shared/google/jest.config.ts"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"build": {
|
|
25
|
+
"executor": "@nx/esbuild:esbuild",
|
|
26
|
+
"outputs": ["{options.outputPath}"],
|
|
27
|
+
"options": {
|
|
28
|
+
"outputPath": "dist/packages/shared/google",
|
|
29
|
+
"tsConfig": "packages/shared/google/tsconfig.lib.json",
|
|
30
|
+
"packageJson": "packages/shared/google/package.json",
|
|
31
|
+
"main": "packages/shared/google/src/index.ts",
|
|
32
|
+
"assets": ["packages/shared/google/*.md"]
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"tags": []
|
|
37
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './lib/google.service';
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { Test, TestingModule } from '@nestjs/testing';
|
|
2
|
+
import { HttpService } from '@nestjs/axios';
|
|
3
|
+
import { GoogleService } from './google.service';
|
|
4
|
+
import { of } from 'rxjs';
|
|
5
|
+
|
|
6
|
+
describe('GoogleService', () => {
|
|
7
|
+
let service: GoogleService;
|
|
8
|
+
let httpService: HttpService;
|
|
9
|
+
|
|
10
|
+
const mockHttpService = {
|
|
11
|
+
get: jest.fn(),
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
beforeEach(async () => {
|
|
15
|
+
const module: TestingModule = await Test.createTestingModule({
|
|
16
|
+
providers: [
|
|
17
|
+
GoogleService,
|
|
18
|
+
{
|
|
19
|
+
provide: HttpService,
|
|
20
|
+
useValue: mockHttpService,
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
}).compile();
|
|
24
|
+
|
|
25
|
+
service = module.get<GoogleService>(GoogleService);
|
|
26
|
+
httpService = module.get<HttpService>(HttpService);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
jest.clearAllMocks();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should be defined', () => {
|
|
34
|
+
expect(service).toBeDefined();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('getUserId', () => {
|
|
38
|
+
it('should return user ID from Google API', async () => {
|
|
39
|
+
const mockToken = 'test-token';
|
|
40
|
+
const mockResponse = {
|
|
41
|
+
data: {
|
|
42
|
+
user_id: '123456789',
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
mockHttpService.get.mockReturnValue(of(mockResponse));
|
|
47
|
+
|
|
48
|
+
const result = await service.getUserId(mockToken);
|
|
49
|
+
|
|
50
|
+
expect(result).toBe('123456789');
|
|
51
|
+
expect(mockHttpService.get).toHaveBeenCalledWith(
|
|
52
|
+
'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' +
|
|
53
|
+
mockToken,
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should throw error when API call fails', async () => {
|
|
58
|
+
const mockToken = 'invalid-token';
|
|
59
|
+
mockHttpService.get.mockImplementation(() => {
|
|
60
|
+
throw new Error('API Error');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
await expect(service.getUserId(mockToken)).rejects.toThrow('API Error');
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe('getData', () => {
|
|
68
|
+
it('should return user data from Google API', async () => {
|
|
69
|
+
const mockToken = 'test-token';
|
|
70
|
+
const mockResponse = {
|
|
71
|
+
data: {
|
|
72
|
+
user_id: '123456789',
|
|
73
|
+
email: 'test@example.com',
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
mockHttpService.get.mockReturnValue(of(mockResponse));
|
|
78
|
+
|
|
79
|
+
const result = await service.getData(mockToken);
|
|
80
|
+
|
|
81
|
+
expect(result).toEqual({
|
|
82
|
+
id: '123456789',
|
|
83
|
+
email: 'test@example.com',
|
|
84
|
+
});
|
|
85
|
+
expect(mockHttpService.get).toHaveBeenCalledWith(
|
|
86
|
+
'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' +
|
|
87
|
+
mockToken,
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should throw error when API call fails', async () => {
|
|
92
|
+
const mockToken = 'invalid-token';
|
|
93
|
+
mockHttpService.get.mockImplementation(() => {
|
|
94
|
+
throw new Error('API Error');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
await expect(service.getData(mockToken)).rejects.toThrow('API Error');
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import { HttpService } from '@nestjs/axios';
|
|
3
|
+
|
|
4
|
+
@Injectable()
|
|
5
|
+
export class GoogleService {
|
|
6
|
+
constructor(private http: HttpService) {}
|
|
7
|
+
|
|
8
|
+
async getUserId(token: string): Promise<string> {
|
|
9
|
+
const { data } = await this.http
|
|
10
|
+
.get(
|
|
11
|
+
'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + token,
|
|
12
|
+
)
|
|
13
|
+
.toPromise();
|
|
14
|
+
|
|
15
|
+
return data.user_id;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async getData(token: string): Promise<{ email; id }> {
|
|
19
|
+
const { data } = await this.http
|
|
20
|
+
.get(
|
|
21
|
+
'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + token,
|
|
22
|
+
)
|
|
23
|
+
.toPromise();
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
id: data.user_id,
|
|
27
|
+
email: data.email,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"outDir": "../../../dist/out-tsc",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"types": ["node"]
|
|
8
|
+
},
|
|
9
|
+
"exclude": ["**/*.spec.ts", "**/*.test.ts", "jest.config.ts"],
|
|
10
|
+
"include": ["**/*.ts"]
|
|
11
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "../../../dist/out-tsc",
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"types": ["jest", "node"]
|
|
7
|
+
},
|
|
8
|
+
"include": [
|
|
9
|
+
"**/*.spec.ts",
|
|
10
|
+
"**/*.test.ts",
|
|
11
|
+
"**/*.spec.tsx",
|
|
12
|
+
"**/*.test.tsx",
|
|
13
|
+
"**/*.spec.js",
|
|
14
|
+
"**/*.test.js",
|
|
15
|
+
"**/*.spec.jsx",
|
|
16
|
+
"**/*.test.jsx",
|
|
17
|
+
"**/*.d.ts",
|
|
18
|
+
"jest.config.ts"
|
|
19
|
+
]
|
|
20
|
+
}
|
package/src/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./lib/google.service";
|
package/src/index.js
DELETED
package/src/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/shared/google/src/index.ts"],"names":[],"mappings":";;;AAAA,+DAAqC"}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GoogleService = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const common_1 = require("@nestjs/common");
|
|
6
|
-
const axios_1 = require("@nestjs/axios");
|
|
7
|
-
let GoogleService = exports.GoogleService = class GoogleService {
|
|
8
|
-
constructor(http) {
|
|
9
|
-
this.http = http;
|
|
10
|
-
}
|
|
11
|
-
async getUserId(token) {
|
|
12
|
-
const { data } = await this.http
|
|
13
|
-
.get('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + token).toPromise();
|
|
14
|
-
return data.user_id;
|
|
15
|
-
}
|
|
16
|
-
async getData(token) {
|
|
17
|
-
const { data } = await this.http
|
|
18
|
-
.get('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + token).toPromise();
|
|
19
|
-
return {
|
|
20
|
-
id: data.user_id,
|
|
21
|
-
email: data.email
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
exports.GoogleService = GoogleService = tslib_1.__decorate([
|
|
26
|
-
(0, common_1.Injectable)(),
|
|
27
|
-
tslib_1.__metadata("design:paramtypes", [axios_1.HttpService])
|
|
28
|
-
], GoogleService);
|
|
29
|
-
//# sourceMappingURL=google.service.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"google.service.js","sourceRoot":"","sources":["../../../../../../libs/shared/google/src/lib/google.service.ts"],"names":[],"mappings":";;;;AAAA,2CAA0C;AAC1C,yCAA0C;AAGnC,IAAM,aAAa,2BAAnB,MAAM,aAAa;IACtB,YAAoB,IAAiB;QAAjB,SAAI,GAAJ,IAAI,CAAa;IAAI,CAAC;IAE1C,KAAK,CAAC,SAAS,CAAC,KAAa;QACzB,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC,IAAI;aAC3B,GAAG,CAAC,8DAA8D,GAAG,KAAK,CAAC,CAAC,SAAS,EAAE,CAAC;QAE7F,OAAO,IAAI,CAAC,OAAO,CAAC;IACxB,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,KAAa;QACvB,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC,IAAI;aAC3B,GAAG,CAAC,8DAA8D,GAAG,KAAK,CAAC,CAAC,SAAS,EAAE,CAAC;QAE7F,OAAO;YACH,EAAE,EAAE,IAAI,CAAC,OAAO;YAChB,KAAK,EAAE,IAAI,CAAC,KAAK;SACpB,CAAC;IACN,CAAC;CACJ,CAAA;wBAnBY,aAAa;IADzB,IAAA,mBAAU,GAAE;6CAEiB,mBAAW;GAD5B,aAAa,CAmBzB"}
|
package/test-setup.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
//# sourceMappingURL=test-setup.js.map
|
package/test-setup.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"test-setup.js","sourceRoot":"","sources":["../../../../libs/shared/google/test-setup.ts"],"names":[],"mappings":""}
|
|
File without changes
|