-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEVEXP-256: Add timezone response plugin (#8)
* DEVEXP-256: Add timezone response plugin
- Loading branch information
1 parent
3b9b806
commit 1c24cdd
Showing
10 changed files
with
139 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './timezone.response'; |
40 changes: 40 additions & 0 deletions
40
packages/sdk-client/src/plugins/timezone/timezone.response.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ResponsePlugin, ResponsePluginContext } from '../core/response-plugin'; | ||
import { PluginRunner } from '../core'; | ||
|
||
const buggyOperationIds: string[] = [ | ||
'GetCallResult', | ||
]; | ||
|
||
const buggyFields: Record<string, string> = { | ||
'GetCallResult': 'timestamp', | ||
}; | ||
|
||
export class TimezoneResponse<V extends Record<string, any> | undefined = Record<string, any>> | ||
implements ResponsePlugin<V | Record<string, any>> { | ||
|
||
public load( | ||
context: ResponsePluginContext, | ||
): PluginRunner<V | Record<string, unknown>, V> { | ||
return { | ||
transform(res: V) { | ||
// HACK to fix a server-side bug: the timestamp is returned without the timezone | ||
if (res && buggyOperationIds.includes(context.operationId) ) { | ||
for (const key in res) { | ||
if (Object.prototype.hasOwnProperty.call(res, key)) { | ||
const buggyKey = buggyFields[context.operationId]; | ||
if (key === buggyKey && typeof res[buggyKey] === 'string') { | ||
const timestampValue = res[key] as string; | ||
const timeZoneRegex = /([+-]\d{2}(:\d{2})?|Z)$/; | ||
if (!timeZoneRegex.test(timestampValue)) { | ||
res[key] = timestampValue + 'Z'; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return res; | ||
}, | ||
}; | ||
} | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
packages/sdk-client/tests/plugins/timezone/timezone.response.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { TimezoneResponse } from '../../../src'; | ||
import { ResponsePluginContext } from '../../../src/plugins/core/response-plugin'; | ||
import { Headers } from 'node-fetch'; | ||
|
||
describe('Timezone response plugin', () => { | ||
|
||
let context: ResponsePluginContext; | ||
const TIMESTAMP_WITH_MISSING_TIMEZONE = '2024-01-09T15:50:24.000'; | ||
const TIMESTAMP_WITH_TIMEZONE = '2024-01-09T15:50:24.000Z'; | ||
const TIMESTAMP_WITH_TIMEZONE_HOURS = '2024-01-09T15:50:24.000+00'; | ||
const TIMESTAMP_WITH_TIMEZONE_HOURS_MINUTES = '2024-01-09T15:50:24.000+00:00'; | ||
|
||
beforeEach(() => { | ||
context = { | ||
operationId: 'GetCallResult', | ||
apiName: '', | ||
url: '', | ||
requestOptions: { | ||
headers: new Headers(), | ||
basePath: '', | ||
}, | ||
}; | ||
}); | ||
|
||
it('should update the timestamp if the timezone is missing', async () => { | ||
const apiResponse = { | ||
timestamp: TIMESTAMP_WITH_MISSING_TIMEZONE, | ||
}; | ||
const plugin = new TimezoneResponse(); | ||
const runner = plugin.load(context); | ||
const result = await runner.transform(apiResponse); | ||
|
||
expect(result.timestamp).toBe(TIMESTAMP_WITH_TIMEZONE); | ||
}); | ||
|
||
it('should NOT update the timestamp if the timezone is already there with "Z" format', async () => { | ||
const apiResponse = { | ||
timestamp: TIMESTAMP_WITH_TIMEZONE, | ||
}; | ||
const plugin = new TimezoneResponse(); | ||
const runner = plugin.load(context); | ||
const result = await runner.transform(apiResponse); | ||
|
||
expect(result.timestamp).toBe(TIMESTAMP_WITH_TIMEZONE); | ||
}); | ||
|
||
it('should NOT update the timestamp if the timezone is already there with "+XX format"', async () => { | ||
const apiResponse = { | ||
timestamp: TIMESTAMP_WITH_TIMEZONE_HOURS, | ||
}; | ||
const plugin = new TimezoneResponse(); | ||
const runner = plugin.load(context); | ||
const result = await runner.transform(apiResponse); | ||
|
||
expect(result.timestamp).toBe(TIMESTAMP_WITH_TIMEZONE_HOURS); | ||
}); | ||
|
||
it('should NOT update the timestamp if the timezone is already there with "+XX:XX format"', async () => { | ||
const apiResponse = { | ||
timestamp: TIMESTAMP_WITH_TIMEZONE_HOURS_MINUTES, | ||
}; | ||
const plugin = new TimezoneResponse(); | ||
const runner = plugin.load(context); | ||
const result = await runner.transform(apiResponse); | ||
|
||
expect(result.timestamp).toBe(TIMESTAMP_WITH_TIMEZONE_HOURS_MINUTES); | ||
}); | ||
|
||
it('should NOT update the timestamp if the operationId if not listed', async () => { | ||
context.operationId = 'notListedAsBuggy'; | ||
const apiResponse = { | ||
timestamp: TIMESTAMP_WITH_MISSING_TIMEZONE, | ||
}; | ||
const plugin = new TimezoneResponse(); | ||
const runner = plugin.load(context); | ||
const result = await runner.transform(apiResponse); | ||
|
||
expect(result.timestamp).toBe(TIMESTAMP_WITH_MISSING_TIMEZONE); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters