Skip to content

Commit 11f16a6

Browse files
committed
fix mappers
1 parent 59b71f0 commit 11f16a6

File tree

10 files changed

+152
-129
lines changed

10 files changed

+152
-129
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @code.store/arcxp-sdk-ts
22

3+
## 4.34.1
4+
5+
### Patch Changes
6+
7+
- fix mapper types and methods
8+
39
## 4.34.0
410

511
### Minor Changes

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@code.store/arcxp-sdk-ts",
3-
"version": "4.34.0",
3+
"version": "4.34.1",
44
"description": "A strongly typed set of ArcXP API's and utilities reduce the amount of work required to develop with ArcXP, starting with reducing the boilerplate code you have to write.",
55
"type": "commonjs",
66
"main": "./dist/index.js",

src/ans/image.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/ans/index.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/index.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import { ArcAPI, ArcAPIType } from './api';
2-
import { ArcError } from './api/error';
3-
import { WsClient } from './api/ws.client';
4-
import { ContentElement, ContentElementType } from './content-elements';
5-
import * as ArcTypes from './types';
6-
import { ArcUtils } from './utils/arc';
7-
import * as ArcAns from './ans';
8-
1+
export * as ArcTypes from './types';
92
export * from './api/identity/types';
103
export * from './api/draft/types';
114
export * from './api/site/types';
@@ -20,4 +13,9 @@ export * from './api/global-settings/types';
2013
export * from './api/tags/types';
2114
export * from './api/content-ops/types';
2215

23-
export { ArcAPI, ArcAPIType, ArcUtils, WsClient, ContentElement, ContentElementType, ArcTypes, ArcError, ArcAns };
16+
export { ArcAPI, ArcAPIType } from './api';
17+
export { ArcUtils } from './utils/arc';
18+
export { ArcError } from './api/error';
19+
export { WsClient } from './api/ws.client';
20+
export { ContentElement, ContentElementType } from './content-elements';
21+
export * as AnsMapper from './mapper';

src/ans/doc.ts renamed to src/mapper/doc.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,26 @@ import type { MaybePromise, Optional } from '../types/utils';
1212
*
1313
* To migrate it call .migrate() method
1414
*/
15-
export abstract class ArcDocument<ANS extends ANSContent> {
15+
export abstract class Document<ANS extends ANSContent> {
1616
public ans: ANS | null = null;
1717
public circulations: CirculationReference[] = [];
1818

1919
async init(): Promise<void> {
2020
// fetch necessary data and validate it here
2121
}
2222

23-
abstract get sourceId(): string;
24-
abstract get sourceType(): string;
25-
abstract get websiteId(): string;
26-
abstract get legacyUrl(): string;
27-
abstract get arcId(): string;
28-
abstract get type(): string;
29-
abstract get groupId(): string;
30-
abstract get version(): ArcTypes.Story.DescribesTheANSVersionOfThisObject;
23+
abstract sourceId(): MaybePromise<string>;
24+
abstract sourceType(): MaybePromise<string>;
25+
abstract websiteId(): MaybePromise<string>;
26+
abstract legacyUrl(): MaybePromise<string>;
27+
abstract arcId(): MaybePromise<string>;
28+
abstract type(): MaybePromise<string>;
29+
abstract groupId(): MaybePromise<string>;
30+
abstract version(): ArcTypes.Story.DescribesTheANSVersionOfThisObject;
3131

3232
abstract getAns(): MaybePromise<ANS>;
33-
abstract post(params: PostANSParams, payload: PostANSPayload): Promise<void>;
3433

35-
async migrate(): Promise<{
34+
async prepare(): Promise<{
3635
params: PostANSParams;
3736
payload: PostANSPayload<ANS>;
3837
}> {
@@ -41,8 +40,6 @@ export abstract class ArcDocument<ANS extends ANSContent> {
4140
const payload = await this.payload();
4241
const params = await this.params();
4342

44-
await this.post(params, payload);
45-
4643
return { payload, params };
4744
}
4845

@@ -51,35 +48,35 @@ export abstract class ArcDocument<ANS extends ANSContent> {
5148
this.circulations = await this.getCirculations();
5249

5350
return {
54-
sourceId: this.sourceId,
55-
sourceType: this.sourceType,
51+
sourceId: await this.sourceId(),
52+
sourceType: await this.sourceType(),
5653
ANS: this.ans,
5754
circulations: this.circulations,
58-
arcAdditionalProperties: this.additionalProperties,
55+
arcAdditionalProperties: this.additionalProperties(),
5956
};
6057
}
6158

6259
private async params(): Promise<PostANSParams> {
63-
if (!this.websiteId) {
60+
if (!this.websiteId()) {
6461
throw new Error('Website is not initialized! get params() should be called after payload()!');
6562
}
6663

6764
return {
68-
website: this.websiteId,
69-
groupId: this.groupId,
70-
priority: this.priority,
65+
website: await this.websiteId(),
66+
groupId: await this.groupId(),
67+
priority: this.priority(),
7168
};
7269
}
7370

74-
protected get additionalProperties(): PostANSPayload['arcAdditionalProperties'] {
71+
protected additionalProperties(): PostANSPayload['arcAdditionalProperties'] {
7572
return {
7673
story: {
7774
publish: false,
7875
},
7976
};
8077
}
8178

82-
protected get priority(): 'historical' | 'live' {
79+
protected priority(): 'historical' | 'live' {
8380
return 'historical';
8481
}
8582

@@ -95,11 +92,11 @@ export abstract class ArcDocument<ANS extends ANSContent> {
9592
return;
9693
}
9794

98-
protected getSource(): Optional<ArcTypes.Story.Source> {
95+
protected async getSource(): Promise<Optional<ArcTypes.Story.Source>> {
9996
return {
10097
name: 'code-store',
10198
system: 'code-store',
102-
source_id: this.sourceId,
99+
source_id: await this.sourceId(),
103100
};
104101
}
105102

src/mapper/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { Document } from './doc';
2+
export { Story } from './story';

src/ans/story.ts renamed to src/mapper/story.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type { Story } from '../types';
2-
import { ArcDocument } from './doc';
1+
import type { ANSContent } from '../api/migration-center/types';
2+
import type * as Types from '../types';
3+
import { Document } from './doc';
34

45
/**
56
* Base class for all arc stories, it provides common methods and properties
@@ -18,11 +19,15 @@ import { ArcDocument } from './doc';
1819
* }];
1920
* }
2021
*/
21-
export abstract class ArcStory extends ArcDocument<Story.AStory> {
22-
type = 'story' as const;
22+
export abstract class Story<ANS extends ANSContent = Types.Story.AStory> extends Document<ANS> {
23+
type() {
24+
return 'story' as const;
25+
}
2326

2427
async getAns() {
25-
const id = this.arcId;
28+
const id = await this.arcId();
29+
const version = this.version();
30+
const type = this.type();
2631
const publicationDate = this.getPublicationDate();
2732
const headlines = this.getHeadlines();
2833
const subheadlines = this.getSubheadlines();
@@ -38,13 +43,17 @@ export abstract class ArcStory extends ArcDocument<Story.AStory> {
3843
const promoItems = await this.getPromoItems();
3944
const contentElements = await this.getContentElements();
4045
const webskedStatusCode = await this.getWebskedStatusCode();
46+
const websiteId = await this.websiteId();
47+
const source = await this.getSource();
48+
const comments = await this.getComments();
49+
const legacyUrl = await this.legacyUrl();
4150

4251
return {
43-
type: this.type,
52+
type,
4453
_id: id,
45-
version: this.version,
46-
website: this.websiteId,
47-
canonical_website: this.websiteId,
54+
version,
55+
website: websiteId,
56+
canonical_website: websiteId,
4857
language,
4958
subtype,
5059
label,
@@ -62,8 +71,8 @@ export abstract class ArcStory extends ArcDocument<Story.AStory> {
6271
first_publish_date: this.formatDate(publicationDate),
6372
publish_date: this.formatDate(publicationDate),
6473
display_date: this.formatDate(this.getDisplayDate()),
65-
source: this.getSource(),
66-
comments: this.getComments(),
74+
source,
75+
comments,
6776
taxonomy: {
6877
tags,
6978
},
@@ -72,8 +81,8 @@ export abstract class ArcStory extends ArcDocument<Story.AStory> {
7281
},
7382
content_elements: contentElements,
7483
additional_properties: {
75-
url: this.legacyUrl,
84+
url: legacyUrl,
7685
},
77-
};
86+
} as unknown as ANS;
7887
}
7988
}

src/tests/ans/mapper.test.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { beforeEach, describe, expect, test, vi } from 'vitest';
2+
import { ContentElement } from '../../content-elements';
3+
import { Story } from '../../mapper';
4+
5+
describe('Mapper', () => {
6+
const ans = {
7+
type: 'story',
8+
_id: 'arc_id',
9+
version: '1.0.0',
10+
website: 'website_id',
11+
canonical_website: 'website_id',
12+
language: 'en-GB',
13+
subtype: 'subtype',
14+
credits: { by: [] },
15+
headlines: { basic: '' },
16+
subheadlines: { basic: '' },
17+
description: { basic: '' },
18+
created_date: expect.any(String),
19+
first_publish_date: expect.any(String),
20+
publish_date: expect.any(String),
21+
display_date: expect.any(String),
22+
source: { name: 'code-store', system: 'code-store', source_id: 'source_id' },
23+
taxonomy: { tags: [] },
24+
workflow: expect.any(Object),
25+
content_elements: [{ type: 'text', content: '', alignment: 'left' }],
26+
additional_properties: { url: 'legacy_url' },
27+
};
28+
29+
const mocks = {
30+
init: vi.fn().mockResolvedValue(undefined),
31+
getSourceId: vi.fn().mockResolvedValue('source_id'),
32+
getSourceType: vi.fn().mockResolvedValue('source_type'),
33+
getWebsiteId: vi.fn().mockResolvedValue('website_id'),
34+
getLegacyUrl: vi.fn().mockResolvedValue('legacy_url'),
35+
getArcId: vi.fn().mockResolvedValue('arc_id'),
36+
getGroupId: vi.fn().mockResolvedValue('group_id'),
37+
getSubtype: vi.fn().mockResolvedValue('subtype'),
38+
getContentElements: vi.fn().mockResolvedValue([ContentElement.text('')]),
39+
};
40+
41+
class CodeStory extends Story {
42+
version = () => '1.0.0' as any;
43+
groupId = mocks.getGroupId;
44+
sourceId = mocks.getSourceId;
45+
sourceType = mocks.getSourceType;
46+
websiteId = mocks.getWebsiteId;
47+
legacyUrl = mocks.getLegacyUrl;
48+
arcId = mocks.getArcId;
49+
50+
init = mocks.init;
51+
getSubtype = mocks.getSubtype;
52+
getContentElements = mocks.getContentElements;
53+
}
54+
55+
beforeEach(() => {
56+
vi.clearAllMocks();
57+
});
58+
59+
test('Should return valid ANS', async () => {
60+
const story = new CodeStory();
61+
62+
const ans = await story.getAns();
63+
64+
expect(ans).toBeDefined();
65+
expect(ans.type).toBe('story');
66+
expect(ans._id).toBe('arc_id');
67+
expect(ans.version).toBe('1.0.0');
68+
});
69+
70+
test('prepare() should call params, payload, getAns', async () => {
71+
const story = new CodeStory();
72+
const getAns = vi.spyOn(story, 'getAns');
73+
const payload = vi.spyOn(story, 'payload' as any);
74+
const params = vi.spyOn(story, 'params' as any);
75+
const result = await story.prepare();
76+
77+
expect(mocks.init).toBeCalledTimes(1);
78+
expect(getAns).toBeCalledTimes(1);
79+
expect(payload).toBeCalledTimes(1);
80+
expect(params).toBeCalledTimes(1);
81+
expect(mocks.getContentElements).toBeCalledTimes(1);
82+
83+
expect(result.params).toBeDefined();
84+
expect(result.params.website).toBe('website_id');
85+
expect(result.params.groupId).toBe('group_id');
86+
expect(result.params.priority).toBe('historical');
87+
88+
expect(result.payload).toBeDefined();
89+
expect(result.payload.sourceId).toBe('source_id');
90+
expect(result.payload.sourceType).toBe('source_type');
91+
expect(result.payload.ANS).toMatchObject(ans);
92+
});
93+
});

0 commit comments

Comments
 (0)