Browse Source

Fix jsonpath import

pull/6129/head
Thomas Kaul 1 month ago
parent
commit
604eddc237
  1. 29
      apps/api/src/services/data-provider/manual/manual.service.spec.ts
  2. 17
      apps/api/src/services/data-provider/manual/manual.service.ts
  3. 8
      package-lock.json
  4. 1
      package.json

29
apps/api/src/services/data-provider/manual/manual.service.spec.ts

@ -0,0 +1,29 @@
import { ManualService } from './manual.service';
describe('ManualService', () => {
let manualService: ManualService;
beforeEach(() => {
manualService = new ManualService(null, null, null);
});
describe('extractValueFromJson', () => {
it('should extract market price from stock API response', () => {
const data = {
currency: 'USD',
market: {
previousClose: 273.04,
price: 271.86
},
symbol: 'AAPL'
};
const result = manualService.extractValueFromJson({
data,
pathExpression: '$.market.price'
});
expect(result).toBe('271.86');
});
});
});

17
apps/api/src/services/data-provider/manual/manual.service.ts

@ -26,7 +26,7 @@ import { Injectable, Logger } from '@nestjs/common';
import { DataSource, SymbolProfile } from '@prisma/client';
import * as cheerio from 'cheerio';
import { addDays, format, isBefore } from 'date-fns';
import * as jsonpath from 'jsonpath';
import jsonpath from 'jsonpath';
@Injectable()
export class ManualService implements DataProviderInterface {
@ -40,6 +40,16 @@ export class ManualService implements DataProviderInterface {
return true;
}
public extractValueFromJson({
data,
pathExpression
}: {
data: object;
pathExpression: string;
}) {
return String(jsonpath.query(data, pathExpression)[0]);
}
public async getAssetProfile({
symbol
}: GetAssetProfileParams): Promise<Partial<SymbolProfile>> {
@ -288,7 +298,10 @@ export class ManualService implements DataProviderInterface {
if (response.headers.get('content-type')?.includes('application/json')) {
const data = await response.json();
value = String(jsonpath.query(data, scraperConfiguration.selector)[0]);
value = this.extractValueFromJson({
data,
pathExpression: scraperConfiguration.selector
});
} else {
const $ = cheerio.load(await response.text());

8
package-lock.json

@ -124,6 +124,7 @@
"@types/big.js": "6.2.2",
"@types/google-spreadsheet": "3.1.5",
"@types/jest": "30.0.0",
"@types/jsonpath": "0.2.4",
"@types/lodash": "4.17.20",
"@types/node": "22.15.17",
"@types/papaparse": "5.3.7",
@ -12436,6 +12437,13 @@
"dev": true,
"license": "MIT"
},
"node_modules/@types/jsonpath": {
"version": "0.2.4",
"resolved": "https://registry.npmjs.org/@types/jsonpath/-/jsonpath-0.2.4.tgz",
"integrity": "sha512-K3hxB8Blw0qgW6ExKgMbXQv2UPZBoE2GqLpVY+yr7nMD2Pq86lsuIzyAaiQ7eMqFL5B6di6pxSkogLJEyEHoGA==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/jsonwebtoken": {
"version": "9.0.10",
"resolved": "https://registry.npmjs.org/@types/jsonwebtoken/-/jsonwebtoken-9.0.10.tgz",

1
package.json

@ -168,6 +168,7 @@
"@types/big.js": "6.2.2",
"@types/google-spreadsheet": "3.1.5",
"@types/jest": "30.0.0",
"@types/jsonpath": "0.2.4",
"@types/lodash": "4.17.20",
"@types/node": "22.15.17",
"@types/papaparse": "5.3.7",

Loading…
Cancel
Save