Skip to content

Commit 4b26591

Browse files
makdenissmakdeniss
authored andcommitted
fix: address PR comments (#335)
1 parent e03cf56 commit 4b26591

File tree

5 files changed

+40
-37
lines changed

5 files changed

+40
-37
lines changed

projects/lib/services/resource/resource.service.spec.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -679,29 +679,6 @@ describe('ResourceService', () => {
679679
});
680680
});
681681

682-
describe('stripTypename (private)', () => {
683-
it('should return primitives as-is', () => {
684-
// @ts-ignore accessing private method for test
685-
expect((service as any).stripTypename(42)).toBe(42);
686-
// @ts-ignore
687-
expect((service as any).stripTypename('x')).toBe('x');
688-
// @ts-ignore
689-
expect((service as any).stripTypename(null)).toBe(null);
690-
// @ts-ignore
691-
expect((service as any).stripTypename(undefined)).toBe(undefined);
692-
});
693-
694-
it('should clean nested objects and arrays', () => {
695-
const input = {
696-
__typename: 'Root',
697-
a: { __typename: 'A', b: 1 },
698-
arr: [{ __typename: 'X', v: 1 }, [{ __typename: 'Y', z: 2 }, 3]],
699-
} as any;
700-
// @ts-ignore
701-
const output = (service as any).stripTypename(input);
702-
expect(output).toEqual({ a: { b: 1 }, arr: [{ v: 1 }, [{ z: 2 }, 3]] });
703-
});
704-
});
705682

706683
describe('readAccountInfo', () => {
707684
it('should read account info', (done) => {

projects/lib/services/resource/resource.service.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { LuigiCoreService } from '@openmfp/portal-ui-lib';
77
import {
88
getValueByPath,
99
replaceDotsAndHyphensWithUnderscores,
10+
stripTypename,
1011
} from '@platform-mesh/portal-ui-lib/utils';
1112
import { gql } from 'apollo-angular';
1213
import * as gqlBuilder from 'gql-query-builder';
@@ -286,7 +287,7 @@ export class ResourceService {
286287
const kind = resourceDefinition.kind;
287288
const namespace = nodeContext.namespaceId;
288289

289-
const cleanResource = this.stripTypename(resource);
290+
const cleanResource = stripTypename(resource);
290291

291292
const mutation = gqlBuilder.mutation({
292293
operation: group,
@@ -367,17 +368,4 @@ export class ResourceService {
367368
return nodeContext?.resourceDefinition?.scope === 'Namespaced';
368369
}
369370

370-
private stripTypename<T>(value: T): T {
371-
if (Array.isArray(value)) {
372-
return (value as any[]).map((v) => this.stripTypename(v)) as T;
373-
}
374-
if (value && typeof value === 'object') {
375-
const { __typename, ...rest } = value as Record<string, unknown>;
376-
for (const k of Object.keys(rest)) {
377-
rest[k] = this.stripTypename(rest[k]);
378-
}
379-
return rest as T;
380-
}
381-
return value;
382-
}
383371
}

projects/lib/utils/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './columns-to-gql-fields';
22
export * from './get-value-by-path';
33
export * from './group-name-sanitizer';
44
export * from './resource-field-by-path';
5+
export * from './resource-sanitizer';
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { stripTypename } from '@platform-mesh/portal-ui-lib/utils';
2+
3+
describe('stripTypename (utils)', () => {
4+
it('should return primitives as-is', () => {
5+
expect(stripTypename(42 as any)).toBe(42);
6+
expect(stripTypename('x' as any)).toBe('x');
7+
expect(stripTypename(null as any)).toBe(null);
8+
expect(stripTypename(undefined as any)).toBe(undefined);
9+
});
10+
11+
it('should clean nested objects and arrays', () => {
12+
const input = {
13+
__typename: 'Root',
14+
a: { __typename: 'A', b: 1 },
15+
arr: [{ __typename: 'X', v: 1 }, [{ __typename: 'Y', z: 2 }, 3]],
16+
} as any;
17+
18+
const output = stripTypename(input);
19+
expect(output).toEqual({ a: { b: 1 }, arr: [{ v: 1 }, [{ z: 2 }, 3]] });
20+
});
21+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export function stripTypename<T>(value: T): T {
2+
if (Array.isArray(value)) {
3+
return (value as unknown as any[]).map((v) => stripTypename(v)) as T;
4+
}
5+
if (value && typeof value === 'object') {
6+
const { __typename, ...rest } = value as Record<string, unknown> & {
7+
__typename?: string;
8+
};
9+
for (const k of Object.keys(rest)) {
10+
// @ts-ignore - rest is an indexable object
11+
rest[k] = stripTypename((rest as any)[k]);
12+
}
13+
return rest as T;
14+
}
15+
return value;
16+
}

0 commit comments

Comments
 (0)