Skip to content

Commit

Permalink
Safer B2B response types
Browse files Browse the repository at this point in the history
  • Loading branch information
kouak committed Jul 12, 2024
1 parent 2db5792 commit 40cbdf8
Show file tree
Hide file tree
Showing 30 changed files with 437 additions and 301 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-schools-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dgac/nmb2b-client': minor
---

Response typings should be safer now
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
"clean": "rimraf dist",
"build": "tsup-node",
"release": "pnpm build && changeset publish",
"lint": "eslint",
"lint": "eslint --max-warnings=0",
"test": "vitest",
"test:ci": "vitest --watch=false --reporter=basic --reporter=junit --outputFile.junit=junit.xml --coverage.enabled",
"test:ci": "vitest --watch=false --reporter=basic --reporter=junit --outputFile.junit=junit.xml --coverage.enabled --typecheck",
"typecheck": "tsc --noEmit"
},
"files": [
Expand All @@ -97,7 +97,6 @@
"@eslint/js": "^9.6.0",
"@total-typescript/shoehorn": "^0.1.2",
"@types/debug": "^4.1.12",
"@types/eslint__js": "^8.42.3",
"@types/invariant": "^2.2.37",
"@types/node": "^18.19.39",
"@types/proper-lockfile": "^4.1.4",
Expand All @@ -122,7 +121,8 @@
"proper-lockfile": "^4.1.2",
"remeda": "^2.5.0",
"soap": "^1.0.4",
"tar": "^7.4.0"
"tar": "^7.4.0",
"type-fest": "^4.21.0"
},
"publishConfig": {
"provenance": true
Expand Down
26 changes: 3 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/Airspace/queryCompleteAIXMDatasets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ describe('queryCompleteAIXMDatasets', async () => {
},
});

expect(res.data.datasetSummaries).toBeDefined();
expect(res.data?.datasetSummaries).toBeDefined();
assert(res.data?.datasetSummaries);
expect(res.data.datasetSummaries.length).toBeGreaterThanOrEqual(1);

const dataset = res.data.datasetSummaries[0];
assert(dataset);
expect(Array.isArray(dataset.files)).toBe(true);
assert(Array.isArray(dataset.files));

expect(dataset.files.length).toBeGreaterThan(0);

dataset.files.forEach((f) => {
expect(f).toMatchObject({
id: expect.stringMatching(/BASELINE\.zip$/),
Expand Down
13 changes: 4 additions & 9 deletions src/Airspace/queryCompleteAIXMDatasets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,20 @@ import { injectSendTime, responseStatusHandler } from '../utils/internals';
import { prepareSerializer } from '../utils/transformers';
import type { AirspaceClient } from './';
import type { AiracIdentifier, AIXMFile } from './types';
import type { CollapseEmptyObjectsToNull } from '../utils/types';

import type {
DateYearMonthDay,
DateYearMonthDayPeriod,
Reply,
ReplyWithData,
} from '../Common/types';

export interface CompleteAIXMDatasetRequest {
queryCriteria: CompleteDatasetQueryCriteria;
}

export type CompleteAIXMDatasetReply = CollapseEmptyObjectsToNull<
Reply & {
data: {
datasetSummaries: CompleteDatasetSummary[];
};
}
>;
export type CompleteAIXMDatasetReply = ReplyWithData<{
datasetSummaries: CompleteDatasetSummary[];
}>;

type Values = CompleteAIXMDatasetRequest;
type Result = CompleteAIXMDatasetReply;
Expand Down
32 changes: 15 additions & 17 deletions src/Airspace/retrieveAUP.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,40 @@ import { assert, beforeAll, describe, expect, test } from 'vitest';
import { makeAirspaceClient } from '..';
import b2bOptions from '../../tests/options';
import { shouldUseRealB2BConnection } from '../../tests/utils';
import type { AUPSummary } from './types';

describe('retrieveAUP', async () => {
const Airspace = await makeAirspaceClient(b2bOptions);

let AUPSummaries: AUPSummary[] = [];
let AUPSummaryIds: Array<string> = [];
beforeAll(async () => {
// Find some AUP id
const res = await Airspace.retrieveAUPChain({
amcIds: ['LFFAZAMC'],
chainDate: new Date(),
});

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- TODO: Check if this condition is necessary ?
if (res.data) {
assert(res.data.chains[0]);
AUPSummaries = res.data.chains[0].aups.filter(
({ aupState }) => aupState === 'RELEASED',
);

AUPSummaries.sort(
(a, b) =>
(a.lastUpdate?.timestamp.valueOf() ?? 0) -
(b.lastUpdate?.timestamp.valueOf() ?? 0),
);
}
assert(res.data?.chains?.[0]?.aups);

const summaries = res.data.chains[0].aups.filter(
({ aupState }) => aupState === 'RELEASED',
);
summaries.sort(
(a, b) =>
(a.lastUpdate?.timestamp.valueOf() ?? 0) -
(b.lastUpdate?.timestamp.valueOf() ?? 0),
);

AUPSummaryIds = summaries.map(({ id }) => id);
});

test.runIf(shouldUseRealB2BConnection)('AUP Retrieval', async () => {
if (!AUPSummaries[0]) {
if (!AUPSummaryIds[0]) {
console.warn('AUPChainRetrieval did not yield any AUP id');
return;
}

const res = await Airspace.retrieveAUP({
aupId: AUPSummaries[0].id,
aupId: AUPSummaryIds[0],
returnComputed: true,
});

Expand Down
21 changes: 8 additions & 13 deletions src/Airspace/retrieveAUP.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { AirspaceClient } from './';
import { injectSendTime, responseStatusHandler } from '../utils/internals';
import type { ReplyWithData } from '../Common/types';
import type { SoapOptions } from '../soap';
import { prepareSerializer } from '../utils/transformers';
import { instrument } from '../utils/instrumentation';
import type { AUPId, AUP } from './types';
import type { Reply } from '../Common/types';
import type { CollapseEmptyObjectsToNull } from '../utils/types';
import { injectSendTime, responseStatusHandler } from '../utils/internals';
import { prepareSerializer } from '../utils/transformers';
import type { AirspaceClient } from './';
import type { AUP, AUPId } from './types';

type Values = AUPRetrievalRequest;
type Result = AUPRetrievalReply;
Expand Down Expand Up @@ -44,10 +43,6 @@ export interface AUPRetrievalRequest {
returnComputed?: boolean;
}

export type AUPRetrievalReply = CollapseEmptyObjectsToNull<
Reply & {
data: {
aup: AUP;
};
}
>;
export type AUPRetrievalReply = ReplyWithData<{
aup: AUP;
}>;
2 changes: 1 addition & 1 deletion src/Airspace/retrieveAUPChain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ describe('retrieveAUPChain', async () => {
chainDate: new Date(),
});

expect(Array.isArray(res.data.chains)).toBe(true);
expect(Array.isArray(res.data?.chains)).toBe(true);
});
});
21 changes: 8 additions & 13 deletions src/Airspace/retrieveAUPChain.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import type {
AirNavigationUnitId,
DateYearMonthDay,
ReplyWithData,
} from '../Common/types';
import type { SoapOptions } from '../soap';
import { instrument } from '../utils/instrumentation';
import { injectSendTime, responseStatusHandler } from '../utils/internals';
import { prepareSerializer } from '../utils/transformers';
import type { AirspaceClient } from './';
import type {
AirNavigationUnitId,
DateYearMonthDay,
Reply,
} from '../Common/types';

import type { AUPChain } from './types';
import type { CollapseEmptyObjectsToNull } from '../utils/types';

type Values = AUPChainRetrievalRequest;
type Result = AUPChainRetrievalReply;
Expand Down Expand Up @@ -51,10 +50,6 @@ export interface AUPChainRetrievalRequest {
amcIds?: AirNavigationUnitId[];
}

export type AUPChainRetrievalReply = CollapseEmptyObjectsToNull<
Reply & {
data: {
chains: AUPChain[];
};
}
>;
export type AUPChainRetrievalReply = ReplyWithData<{
chains: AUPChain[];
}>;
2 changes: 1 addition & 1 deletion src/Airspace/retrieveEAUPChain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('retrieveEAUPChain', async () => {
eaups: expect.any(Array),
});

for (const eaup of res.data.chain.eaups) {
for (const eaup of res.data.chain.eaups ?? []) {
expect(eaup).toEqual({
releaseTime: expect.any(Date),
validityPeriod: {
Expand Down
13 changes: 4 additions & 9 deletions src/Airspace/retrieveEAUPChain.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { DateYearMonthDay, ReplyWithData } from '../Common/types';
import type { SoapOptions } from '../soap';
import { instrument } from '../utils/instrumentation';
import { injectSendTime, responseStatusHandler } from '../utils/internals';
import { prepareSerializer } from '../utils/transformers';
import type { AirspaceClient } from './';
import type { DateYearMonthDay, Reply } from '../Common/types';

import type { CollapseEmptyObjectsToNull } from '../utils/types';
import type { EAUPChain } from './types';

type Values = EAUPChainRetrievalRequest;
Expand Down Expand Up @@ -46,10 +45,6 @@ export interface EAUPChainRetrievalRequest {
chainDate: DateYearMonthDay;
}

export type EAUPChainRetrievalReply = CollapseEmptyObjectsToNull<
Reply & {
data: {
chain: EAUPChain;
};
}
>;
export type EAUPChainRetrievalReply = ReplyWithData<{
chain: EAUPChain;
}>;
8 changes: 7 additions & 1 deletion src/Common/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { SoapDeserializer } from '../utils/types';

export type DateYearMonthDay = Date;
export type DateTimeMinute = Date;
export type DateTimeSecond = Date;
Expand Down Expand Up @@ -128,7 +130,7 @@ export type ReplyStatus =
| 'CONFLICTING_UPDATE'
| 'INVALID_DATASET';

export type Reply = {
export interface Reply {
requestReceptionTime?: DateTimeSecond;
requestId?: string;
sendTime?: DateTimeSecond;
Expand All @@ -140,6 +142,10 @@ export type Reply = {
reason?: string;
}

export type ReplyWithData<TData = never> = Reply & {
data: SoapDeserializer<TData>;
};

export type Request = {
endUserId?: string;
onBehalfOfUnit?: AirNavigationUnitId;
Expand Down
Loading

0 comments on commit 40cbdf8

Please sign in to comment.