From 56c67199562684a2414fbd4a8ff0516153fb3b00 Mon Sep 17 00:00:00 2001 From: Kamal Fariz Mahyuddin Date: Thu, 12 Sep 2024 08:17:51 -0700 Subject: [PATCH] Fix types relating to invite creation (#63) --- package.json | 2 +- src/resources/InviteResource.ts | 43 +++++++++++++++++++++++++------ src/util/json-api/JSONAPIModel.ts | 7 ++--- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index a62071d..fa2b239 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@envoy/envoy-integrations-sdk", - "version": "2.2.0", + "version": "2.2.1", "description": "SDK for building Envoy integrations.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/resources/InviteResource.ts b/src/resources/InviteResource.ts index a044617..648a400 100644 --- a/src/resources/InviteResource.ts +++ b/src/resources/InviteResource.ts @@ -1,3 +1,4 @@ +import JSONAPIData from '../util/json-api/JSONAPIData'; import JSONAPIModel from '../util/json-api/JSONAPIModel'; /** @@ -32,15 +33,15 @@ export interface InviteAttributes { auto_approved: boolean; report: Array<{ reason: string; - result: 'pass' | 'fail' | 'pending', - source: string, + result: 'pass' | 'fail' | 'pending'; + source: string; messages: { failure?: { - text: string, - header: string, - } - } - }> + text: string; + header: string; + }; + }; + }>; }; email?: string; 'expected-arrival-time'?: string; @@ -89,4 +90,30 @@ export type InviteModel = JSONAPIModel; +type InviteCreationRequiredRelationships = 'location'; // surprising, but flow is not required. if not provided, it will be defaulted +type InviteCreationProhibitedRelationships = 'creator'; +type InviteCreationOptionalRelationships = Exclude< + InviteRelationships, + InviteCreationRequiredRelationships | InviteCreationProhibitedRelationships +>; + +/** + * Here we are going to do a little surgery on JSONAPIModel to allow us to specify required and optional relationships. + * We do this by first omitting the relationships field from JSONAPIModel, then adding it back in with modified type. + */ +export type InviteCreationModel = Omit< + JSONAPIModel, + 'relationships' +> & { + relationships: { + [key in InviteCreationRequiredRelationships]: { + data: JSONAPIData | Array; + }; + } & { + [key in InviteCreationOptionalRelationships]?: { + data: JSONAPIData | Array | null; + }; + } & { + [key in InviteCreationProhibitedRelationships]?: never; + }; +}; diff --git a/src/util/json-api/JSONAPIModel.ts b/src/util/json-api/JSONAPIModel.ts index 1c2d707..e172bf5 100644 --- a/src/util/json-api/JSONAPIModel.ts +++ b/src/util/json-api/JSONAPIModel.ts @@ -1,10 +1,11 @@ import JSONAPIData from './JSONAPIData'; -export default interface JSONAPIModel extends JSONAPIData { +export default interface JSONAPIModel + extends JSONAPIData { attributes: Attributes; relationships: { [key in Relationships]: { - data: JSONAPIData & Array + data: JSONAPIData | Array | null; }; - } + }; }