Skip to content

Commit e2f3646

Browse files
authored
fix: strip typenames added to the GQL query result (#352)
* fix: strip typenames added to the GQL query result * fix: lint errors
1 parent 601bddd commit e2f3646

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

src/api/nes.client.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
} from '@herodevs/eol-shared';
99
import { config } from '../config/constants.ts';
1010
import { debugLogger } from '../service/log.svc.ts';
11+
import { stripTypename } from '../utils/strip-typename.ts';
1112
import { createReportMutation, getEolReportQuery } from './gql-operations.ts';
1213

1314
export const createApollo = (uri: string) =>
@@ -70,7 +71,10 @@ export const SbomScanner = (client: ReturnType<typeof createApollo>) => {
7071
throw new Error('Failed to fetch EOL report');
7172
}
7273

73-
return { ...reportMetadata, components };
74+
return stripTypename({
75+
...reportMetadata,
76+
components,
77+
}) as EolReport;
7478
};
7579
};
7680

src/utils/strip-typename.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export function stripTypename<T>(obj: T): T {
2+
if (Array.isArray(obj)) {
3+
return obj.map(stripTypename) as any;
4+
}
5+
6+
if (obj !== null && typeof obj === 'object') {
7+
const result: any = {};
8+
9+
for (const key of Object.keys(obj)) {
10+
if (key === '__typename') continue;
11+
result[key] = stripTypename((obj as any)[key]);
12+
}
13+
14+
return result;
15+
}
16+
17+
return obj;
18+
}

test/api/strip-typename.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import assert from 'node:assert';
2+
import { describe, it } from 'node:test';
3+
import { stripTypename } from '../../src/utils/strip-typename';
4+
5+
describe('stripTypename', () => {
6+
it('removes __typename from objects and nested structures', () => {
7+
const input = {
8+
__typename: 'Root',
9+
id: '1',
10+
name: 'Test',
11+
nested: {
12+
__typename: 'Nested',
13+
value: 42,
14+
array: [
15+
{ __typename: 'Item', itemId: 'a' },
16+
{ __typename: 'Item', itemId: 'b' },
17+
],
18+
},
19+
list: [
20+
{ __typename: 'ListItem', listId: 'x' },
21+
{ __typename: 'ListItem', listId: 'y' },
22+
],
23+
};
24+
25+
const expectedOutput = {
26+
id: '1',
27+
name: 'Test',
28+
nested: {
29+
value: 42,
30+
array: [{ itemId: 'a' }, { itemId: 'b' }],
31+
},
32+
list: [{ listId: 'x' }, { listId: 'y' }],
33+
};
34+
35+
const output = stripTypename(input);
36+
assert.deepStrictEqual(output, expectedOutput);
37+
});
38+
39+
it('handles null and primitive values correctly', () => {
40+
assert.strictEqual(stripTypename(null), null);
41+
assert.strictEqual(stripTypename(42), 42);
42+
assert.strictEqual(stripTypename('string'), 'string');
43+
assert.strictEqual(stripTypename(true), true);
44+
});
45+
46+
it('handles arrays of primitives correctly', () => {
47+
const input = [1, 2, 3, { __typename: 'Item', id: 'a' }];
48+
const expectedOutput = [1, 2, 3, { id: 'a' }];
49+
const output = stripTypename(input);
50+
assert.deepStrictEqual(output, expectedOutput);
51+
});
52+
});

0 commit comments

Comments
 (0)