opticedge-cloud-utils 1.1.31 → 1.1.32
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/dist/pricecharting.d.ts +1 -0
- package/dist/pricecharting.js +82 -0
- package/package.json +46 -46
- package/src/pricecharting.ts +101 -0
- package/tests/pricecharting.test.ts +139 -2
package/dist/pricecharting.d.ts
CHANGED
|
@@ -15,4 +15,5 @@ type CardParts = {
|
|
|
15
15
|
* 3. If no valid number found — returns `{ name: original, number: null }`.
|
|
16
16
|
*/
|
|
17
17
|
export declare function splitNameAndNumber(input: string, realNumber?: string | null): CardParts;
|
|
18
|
+
export declare function productExists(productId: number, token: string, fetchTimeoutMs: number): Promise<boolean | null>;
|
|
18
19
|
export {};
|
package/dist/pricecharting.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// src/pricecharting.ts
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
4
|
exports.splitNameAndNumber = splitNameAndNumber;
|
|
5
|
+
exports.productExists = productExists;
|
|
6
|
+
const logger_1 = require("./logger");
|
|
4
7
|
const regex_1 = require("./regex");
|
|
8
|
+
const PRODUCT_NOT_FOUND_MESSAGE = 'no such product';
|
|
9
|
+
const PRODUCT_API_URL = 'https://www.pricecharting.com/api/product';
|
|
5
10
|
/**
|
|
6
11
|
* Split a card label into `{ name, number }`.
|
|
7
12
|
*
|
|
@@ -48,3 +53,80 @@ function splitNameAndNumber(input, realNumber) {
|
|
|
48
53
|
return { name: s, number: null };
|
|
49
54
|
return { name: namePart, number: numberPart };
|
|
50
55
|
}
|
|
56
|
+
async function productExists(productId, token, fetchTimeoutMs) {
|
|
57
|
+
if (!Number.isInteger(productId) || productId <= 0) {
|
|
58
|
+
(0, logger_1.log)(logger_1.LogLevel.WARNING, '[productExists] invalid productId', { productId });
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
if (!token?.trim()) {
|
|
62
|
+
(0, logger_1.log)(logger_1.LogLevel.WARNING, '[productExists] missing token', { productId });
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
if (!Number.isFinite(fetchTimeoutMs) || fetchTimeoutMs < 1000) {
|
|
66
|
+
(0, logger_1.log)(logger_1.LogLevel.WARNING, '[productExists] invalid fetch timeout', {
|
|
67
|
+
productId,
|
|
68
|
+
fetchTimeoutMs
|
|
69
|
+
});
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
const params = new URLSearchParams({
|
|
73
|
+
t: token,
|
|
74
|
+
id: String(productId)
|
|
75
|
+
});
|
|
76
|
+
const controller = new AbortController();
|
|
77
|
+
const timeout = setTimeout(() => controller.abort(), fetchTimeoutMs);
|
|
78
|
+
try {
|
|
79
|
+
const resp = await fetch(`${PRODUCT_API_URL}?${params.toString()}`, {
|
|
80
|
+
method: 'GET',
|
|
81
|
+
signal: controller.signal
|
|
82
|
+
});
|
|
83
|
+
clearTimeout(timeout);
|
|
84
|
+
if (resp.status === 404)
|
|
85
|
+
return false;
|
|
86
|
+
if (resp.status !== 200) {
|
|
87
|
+
(0, logger_1.log)(logger_1.LogLevel.WARNING, '[productExists] unexpected status code', {
|
|
88
|
+
productId,
|
|
89
|
+
statusCode: resp.status
|
|
90
|
+
});
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
let data;
|
|
94
|
+
try {
|
|
95
|
+
data = await resp.json();
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
(0, logger_1.log)(logger_1.LogLevel.WARNING, '[productExists] failed to parse JSON', {
|
|
99
|
+
productId,
|
|
100
|
+
traceback: err instanceof Error ? err.stack : String(err)
|
|
101
|
+
});
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
if (typeof data !== 'object' || data === null) {
|
|
105
|
+
(0, logger_1.log)(logger_1.LogLevel.WARNING, '[productExists] unexpected response body', {
|
|
106
|
+
productId,
|
|
107
|
+
bodyType: typeof data
|
|
108
|
+
});
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
const status = (data.status || '').toLowerCase();
|
|
112
|
+
const errText = ((data.error || '') + ' ' + (data['error-message'] || '')).toLowerCase();
|
|
113
|
+
if (status === 'error') {
|
|
114
|
+
if (errText.includes(PRODUCT_NOT_FOUND_MESSAGE.toLowerCase()))
|
|
115
|
+
return false;
|
|
116
|
+
(0, logger_1.log)(logger_1.LogLevel.WARNING, '[productExists] API returned error', { productId, error: errText });
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
catch (unknownErr) {
|
|
122
|
+
const err = unknownErr instanceof Error ? unknownErr : new Error(String(unknownErr));
|
|
123
|
+
(0, logger_1.log)(logger_1.LogLevel.WARNING, '[productExists] request failed', {
|
|
124
|
+
productId,
|
|
125
|
+
traceback: err.stack ?? err.message
|
|
126
|
+
});
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
clearTimeout(timeout);
|
|
131
|
+
}
|
|
132
|
+
}
|
package/package.json
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "opticedge-cloud-utils",
|
|
3
|
-
"version": "1.1.
|
|
4
|
-
"description": "Common utilities for cloud functions",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"clean": "rimraf dist coverage *.tsbuildinfo",
|
|
9
|
-
"build": "npm run clean && tsc",
|
|
10
|
-
"prepare": "npm run build",
|
|
11
|
-
"test": "jest --coverage",
|
|
12
|
-
"lint": "eslint . --ext .ts --fix",
|
|
13
|
-
"format": "prettier --write .",
|
|
14
|
-
"fix": "npm run lint && npm run format"
|
|
15
|
-
},
|
|
16
|
-
"author": "Evans Musonda",
|
|
17
|
-
"license": "MIT",
|
|
18
|
-
"dependencies": {
|
|
19
|
-
"@google-cloud/pubsub": "^5.2.0",
|
|
20
|
-
"@google-cloud/secret-manager": "^6.0.1",
|
|
21
|
-
"@google-cloud/tasks": "^6.1.0",
|
|
22
|
-
"axios": "^1.10.0"
|
|
23
|
-
},
|
|
24
|
-
"devDependencies": {
|
|
25
|
-
"@google-cloud/functions-framework": "*",
|
|
26
|
-
"@types/jest": "^29.5.14",
|
|
27
|
-
"@types/node": "^22.15.23",
|
|
28
|
-
"@typescript-eslint/eslint-plugin": "^8.33.0",
|
|
29
|
-
"@typescript-eslint/parser": "^8.33.0",
|
|
30
|
-
"eslint": "^8.57.1",
|
|
31
|
-
"eslint-config-prettier": "^10.1.5",
|
|
32
|
-
"eslint-plugin-jest": "^28.11.1",
|
|
33
|
-
"eslint-plugin-prettier": "^5.4.0",
|
|
34
|
-
"google-auth-library": "*",
|
|
35
|
-
"jest": "^29.7.0",
|
|
36
|
-
"mongodb": "*",
|
|
37
|
-
"prettier": "^3.5.3",
|
|
38
|
-
"ts-jest": "^29.3.4",
|
|
39
|
-
"typescript": "^5.8.3"
|
|
40
|
-
},
|
|
41
|
-
"peerDependencies": {
|
|
42
|
-
"@google-cloud/functions-framework": "^5.0.2",
|
|
43
|
-
"google-auth-library": "^10.6.2",
|
|
44
|
-
"mongodb": "^7.1.1"
|
|
45
|
-
}
|
|
46
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "opticedge-cloud-utils",
|
|
3
|
+
"version": "1.1.32",
|
|
4
|
+
"description": "Common utilities for cloud functions",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"clean": "rimraf dist coverage *.tsbuildinfo",
|
|
9
|
+
"build": "npm run clean && tsc",
|
|
10
|
+
"prepare": "npm run build",
|
|
11
|
+
"test": "jest --coverage",
|
|
12
|
+
"lint": "eslint . --ext .ts --fix",
|
|
13
|
+
"format": "prettier --write .",
|
|
14
|
+
"fix": "npm run lint && npm run format"
|
|
15
|
+
},
|
|
16
|
+
"author": "Evans Musonda",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@google-cloud/pubsub": "^5.2.0",
|
|
20
|
+
"@google-cloud/secret-manager": "^6.0.1",
|
|
21
|
+
"@google-cloud/tasks": "^6.1.0",
|
|
22
|
+
"axios": "^1.10.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@google-cloud/functions-framework": "*",
|
|
26
|
+
"@types/jest": "^29.5.14",
|
|
27
|
+
"@types/node": "^22.15.23",
|
|
28
|
+
"@typescript-eslint/eslint-plugin": "^8.33.0",
|
|
29
|
+
"@typescript-eslint/parser": "^8.33.0",
|
|
30
|
+
"eslint": "^8.57.1",
|
|
31
|
+
"eslint-config-prettier": "^10.1.5",
|
|
32
|
+
"eslint-plugin-jest": "^28.11.1",
|
|
33
|
+
"eslint-plugin-prettier": "^5.4.0",
|
|
34
|
+
"google-auth-library": "*",
|
|
35
|
+
"jest": "^29.7.0",
|
|
36
|
+
"mongodb": "*",
|
|
37
|
+
"prettier": "^3.5.3",
|
|
38
|
+
"ts-jest": "^29.3.4",
|
|
39
|
+
"typescript": "^5.8.3"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@google-cloud/functions-framework": "^5.0.2",
|
|
43
|
+
"google-auth-library": "^10.6.2",
|
|
44
|
+
"mongodb": "^7.1.1"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/pricecharting.ts
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
|
+
// src/pricecharting.ts
|
|
2
|
+
|
|
3
|
+
import { log, LogLevel } from './logger'
|
|
1
4
|
import { escapeForRegex } from './regex'
|
|
2
5
|
|
|
3
6
|
type CardParts = { name: string; number: string | null }
|
|
4
7
|
|
|
8
|
+
interface ProductExistsResponse {
|
|
9
|
+
status?: string
|
|
10
|
+
error?: string
|
|
11
|
+
'error-message'?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const PRODUCT_NOT_FOUND_MESSAGE = 'no such product'
|
|
15
|
+
const PRODUCT_API_URL = 'https://www.pricecharting.com/api/product'
|
|
16
|
+
|
|
5
17
|
/**
|
|
6
18
|
* Split a card label into `{ name, number }`.
|
|
7
19
|
*
|
|
@@ -49,3 +61,92 @@ export function splitNameAndNumber(input: string, realNumber?: string | null): C
|
|
|
49
61
|
|
|
50
62
|
return { name: namePart, number: numberPart }
|
|
51
63
|
}
|
|
64
|
+
|
|
65
|
+
export async function productExists(
|
|
66
|
+
productId: number,
|
|
67
|
+
token: string,
|
|
68
|
+
fetchTimeoutMs: number
|
|
69
|
+
): Promise<boolean | null> {
|
|
70
|
+
if (!Number.isInteger(productId) || productId <= 0) {
|
|
71
|
+
log(LogLevel.WARNING, '[productExists] invalid productId', { productId })
|
|
72
|
+
return null
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!token?.trim()) {
|
|
76
|
+
log(LogLevel.WARNING, '[productExists] missing token', { productId })
|
|
77
|
+
return null
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (!Number.isFinite(fetchTimeoutMs) || fetchTimeoutMs < 1000) {
|
|
81
|
+
log(LogLevel.WARNING, '[productExists] invalid fetch timeout', {
|
|
82
|
+
productId,
|
|
83
|
+
fetchTimeoutMs
|
|
84
|
+
})
|
|
85
|
+
return null
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const params = new URLSearchParams({
|
|
89
|
+
t: token,
|
|
90
|
+
id: String(productId)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
const controller = new AbortController()
|
|
94
|
+
const timeout = setTimeout(() => controller.abort(), fetchTimeoutMs)
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
const resp = await fetch(`${PRODUCT_API_URL}?${params.toString()}`, {
|
|
98
|
+
method: 'GET',
|
|
99
|
+
signal: controller.signal
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
clearTimeout(timeout)
|
|
103
|
+
|
|
104
|
+
if (resp.status === 404) return false
|
|
105
|
+
if (resp.status !== 200) {
|
|
106
|
+
log(LogLevel.WARNING, '[productExists] unexpected status code', {
|
|
107
|
+
productId,
|
|
108
|
+
statusCode: resp.status
|
|
109
|
+
})
|
|
110
|
+
return null
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let data: ProductExistsResponse
|
|
114
|
+
try {
|
|
115
|
+
data = await resp.json()
|
|
116
|
+
} catch (err) {
|
|
117
|
+
log(LogLevel.WARNING, '[productExists] failed to parse JSON', {
|
|
118
|
+
productId,
|
|
119
|
+
traceback: err instanceof Error ? err.stack : String(err)
|
|
120
|
+
})
|
|
121
|
+
return null
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (typeof data !== 'object' || data === null) {
|
|
125
|
+
log(LogLevel.WARNING, '[productExists] unexpected response body', {
|
|
126
|
+
productId,
|
|
127
|
+
bodyType: typeof data
|
|
128
|
+
})
|
|
129
|
+
return null
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const status = (data.status || '').toLowerCase()
|
|
133
|
+
const errText = ((data.error || '') + ' ' + (data['error-message'] || '')).toLowerCase()
|
|
134
|
+
|
|
135
|
+
if (status === 'error') {
|
|
136
|
+
if (errText.includes(PRODUCT_NOT_FOUND_MESSAGE.toLowerCase())) return false
|
|
137
|
+
log(LogLevel.WARNING, '[productExists] API returned error', { productId, error: errText })
|
|
138
|
+
return null
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return true
|
|
142
|
+
} catch (unknownErr: unknown) {
|
|
143
|
+
const err = unknownErr instanceof Error ? unknownErr : new Error(String(unknownErr))
|
|
144
|
+
log(LogLevel.WARNING, '[productExists] request failed', {
|
|
145
|
+
productId,
|
|
146
|
+
traceback: err.stack ?? err.message
|
|
147
|
+
})
|
|
148
|
+
return null
|
|
149
|
+
} finally {
|
|
150
|
+
clearTimeout(timeout)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// tests/
|
|
1
|
+
// tests/pricecharting.test.ts
|
|
2
2
|
|
|
3
|
-
import { splitNameAndNumber } from '../src/pricecharting'
|
|
3
|
+
import { productExists, splitNameAndNumber } from '../src/pricecharting'
|
|
4
4
|
|
|
5
5
|
describe('splitNameAndNumber (legacy fallback and new realNumber param)', () => {
|
|
6
6
|
const legacyCases: Array<[string, { name: string; number: string | null }]> = [
|
|
@@ -73,3 +73,140 @@ describe('splitNameAndNumber (legacy fallback and new realNumber param)', () =>
|
|
|
73
73
|
expect(splitNameAndNumber(input, '123')).toEqual({ name: 'Example', number: 'X123' })
|
|
74
74
|
})
|
|
75
75
|
})
|
|
76
|
+
|
|
77
|
+
describe('productExists', () => {
|
|
78
|
+
const originalFetch = global.fetch
|
|
79
|
+
|
|
80
|
+
beforeEach(() => {
|
|
81
|
+
jest.clearAllMocks()
|
|
82
|
+
jest.useRealTimers()
|
|
83
|
+
global.fetch = jest.fn()
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
afterEach(() => {
|
|
87
|
+
global.fetch = originalFetch
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
function mockFetchResponse(status: number, body: unknown) {
|
|
91
|
+
;(global.fetch as jest.Mock).mockResolvedValue({
|
|
92
|
+
status,
|
|
93
|
+
json: jest.fn().mockResolvedValue(body)
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
test('returns null for invalid productId', async () => {
|
|
98
|
+
const result = await productExists(0, 'token', 30_000)
|
|
99
|
+
|
|
100
|
+
expect(result).toBeNull()
|
|
101
|
+
expect(global.fetch).not.toHaveBeenCalled()
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('returns null for missing token', async () => {
|
|
105
|
+
const result = await productExists(123, '', 30_000)
|
|
106
|
+
|
|
107
|
+
expect(result).toBeNull()
|
|
108
|
+
expect(global.fetch).not.toHaveBeenCalled()
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
test('returns null for invalid timeout', async () => {
|
|
112
|
+
const result = await productExists(123, 'token', 30)
|
|
113
|
+
|
|
114
|
+
expect(result).toBeNull()
|
|
115
|
+
expect(global.fetch).not.toHaveBeenCalled()
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
test('returns false when API responds with HTTP 404', async () => {
|
|
119
|
+
mockFetchResponse(404, {
|
|
120
|
+
error: 'No such product',
|
|
121
|
+
'error-message': 'No such product',
|
|
122
|
+
status: 'error'
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
const result = await productExists(123, 'token', 30_000)
|
|
126
|
+
|
|
127
|
+
expect(result).toBe(false)
|
|
128
|
+
expect(global.fetch).toHaveBeenCalledTimes(1)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('returns true when API responds with 200 and non-error body', async () => {
|
|
132
|
+
mockFetchResponse(200, {
|
|
133
|
+
status: 'success',
|
|
134
|
+
product: {
|
|
135
|
+
id: 123
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
const result = await productExists(123, 'token', 30_000)
|
|
140
|
+
|
|
141
|
+
expect(result).toBe(true)
|
|
142
|
+
expect(global.fetch).toHaveBeenCalledTimes(1)
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
test('returns false when API responds with status error and no such product message', async () => {
|
|
146
|
+
mockFetchResponse(200, {
|
|
147
|
+
status: 'error',
|
|
148
|
+
error: 'No such product',
|
|
149
|
+
'error-message': 'No such product'
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
const result = await productExists(123, 'token', 30_000)
|
|
153
|
+
|
|
154
|
+
expect(result).toBe(false)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
test('returns null when API responds with non-200 non-404 status', async () => {
|
|
158
|
+
mockFetchResponse(500, {
|
|
159
|
+
status: 'error',
|
|
160
|
+
error: 'server error'
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
const result = await productExists(123, 'token', 30_000)
|
|
164
|
+
|
|
165
|
+
expect(result).toBeNull()
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
test('returns null when JSON parsing fails', async () => {
|
|
169
|
+
;(global.fetch as jest.Mock).mockResolvedValue({
|
|
170
|
+
status: 200,
|
|
171
|
+
json: jest.fn().mockRejectedValue(new Error('invalid json'))
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
const result = await productExists(123, 'token', 30_000)
|
|
175
|
+
|
|
176
|
+
expect(result).toBeNull()
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
test('returns null when response body is not an object', async () => {
|
|
180
|
+
mockFetchResponse(200, null)
|
|
181
|
+
|
|
182
|
+
const result = await productExists(123, 'token', 30_000)
|
|
183
|
+
|
|
184
|
+
expect(result).toBeNull()
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
test('returns null when fetch fails', async () => {
|
|
188
|
+
;(global.fetch as jest.Mock).mockRejectedValue(new Error('network error'))
|
|
189
|
+
|
|
190
|
+
const result = await productExists(123, 'token', 30_000)
|
|
191
|
+
|
|
192
|
+
expect(result).toBeNull()
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
test('calls PriceCharting API with token and product id', async () => {
|
|
196
|
+
mockFetchResponse(200, {
|
|
197
|
+
status: 'success'
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
await productExists(456, 'test-token', 30_000)
|
|
201
|
+
|
|
202
|
+
expect(global.fetch).toHaveBeenCalledTimes(1)
|
|
203
|
+
|
|
204
|
+
const [url, options] = (global.fetch as jest.Mock).mock.calls[0]
|
|
205
|
+
|
|
206
|
+
expect(url).toContain('https://www.pricecharting.com/api/product')
|
|
207
|
+
expect(url).toContain('t=test-token')
|
|
208
|
+
expect(url).toContain('id=456')
|
|
209
|
+
expect(options.method).toBe('GET')
|
|
210
|
+
expect(options.signal).toBeDefined()
|
|
211
|
+
})
|
|
212
|
+
})
|