Skip to content

Commit 8ce7e6a

Browse files
hive-botclaude
andcommitted
fix: type the StyleProfile timestamps as required dates
creationTime and updatedTime were optional, although the API always returns them. They are now required Date fields; a missing or malformed value parses to an invalid Date instead of undefined. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent bad2dcd commit 8ce7e6a

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

‎src/parsing.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,8 +896,9 @@ function parseRawStyleProfile(obj: StyleProfileApiResponse): StyleProfile {
896896
styleRuleIds: parseIdList(obj.style_rule_ids),
897897
translationMemoryIds: parseIdList(obj.translation_memory_ids),
898898
glossaryPriorityOrderEnabled: obj.glossary_priority_order_enabled === true,
899-
creationTime: parseOptionalTimestamp(parseOptionalString(obj.creation_time)),
900-
updatedTime: parseOptionalTimestamp(parseOptionalString(obj.updated_time)),
899+
// Required on the wire; a missing or malformed value gives an invalid Date, never a throw
900+
creationTime: new Date(parseOptionalString(obj.creation_time) ?? NaN),
901+
updatedTime: new Date(parseOptionalString(obj.updated_time) ?? NaN),
901902
};
902903
}
903904

‎src/types.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,10 +1063,10 @@ export interface StyleProfile {
10631063
readonly translationMemoryIds: string[];
10641064
/** Whether the order of glossaryIds sets the priority of the glossaries. */
10651065
readonly glossaryPriorityOrderEnabled: boolean;
1066-
/** Time when the style profile was created; undefined if the API returned no valid time. */
1067-
readonly creationTime?: Date;
1068-
/** Time of the last update; undefined if the API returned no valid time. */
1069-
readonly updatedTime?: Date;
1066+
/** Time when the style profile was created. */
1067+
readonly creationTime: Date;
1068+
/** Time when the style profile was last updated. */
1069+
readonly updatedTime: Date;
10701070
}
10711071

10721072
/**

‎tests/offline/styleProfiles.test.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,13 @@ describe('DeepLClient.getStyleProfile', () => {
221221
expect(fetched).toStrictEqual({ ...profile, description: undefined, industry: undefined });
222222
});
223223

224-
it('tolerates a malformed timestamp and non-string IDs', async () => {
224+
it('tolerates malformed or missing timestamps and non-string IDs', async () => {
225+
const { updated_time: updatedTime, ...withoutUpdatedTime } = profileReply;
226+
void updatedTime;
225227
nock(serverUrl)
226228
.get(`/v3/style_profiles/${profileId}`)
227229
.reply(200, {
228-
...profileReply,
230+
...withoutUpdatedTime,
229231
creation_time: 'not a timestamp',
230232
glossary_ids: ['g-1', 42, null, 'g-2'],
231233
style_rule_ids: null,
@@ -235,9 +237,15 @@ describe('DeepLClient.getStyleProfile', () => {
235237

236238
const fetched = await makeDeeplClient().getStyleProfile(profileId);
237239

240+
// The timestamps are required, so they are invalid Dates rather than undefined
241+
expect(fetched.creationTime).toBeInstanceOf(Date);
242+
expect(fetched.creationTime.getTime()).toBeNaN();
243+
expect(fetched.updatedTime).toBeInstanceOf(Date);
244+
expect(fetched.updatedTime.getTime()).toBeNaN();
238245
expect(fetched).toStrictEqual({
239246
...profile,
240-
creationTime: undefined,
247+
creationTime: fetched.creationTime,
248+
updatedTime: fetched.updatedTime,
241249
glossaryIds: ['g-1', 'g-2'],
242250
styleRuleIds: [],
243251
translationMemoryIds: [],

0 commit comments

Comments
 (0)