Skip to content

Commit 60ce422

Browse files
authored
Fix input types (#17)
* Fix the return type of upserting a customer * Add changeset * Update types to better handle nullable inputs
1 parent 1130174 commit 60ce422

File tree

6 files changed

+286
-244
lines changed

6 files changed

+286
-244
lines changed

.changeset/cyan-stingrays-grab.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@team-plain/typescript-sdk': patch
3+
---
4+
5+
Fix the return type of of upserting a customer (it can not be null)

.changeset/rude-horses-drum.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@team-plain/typescript-sdk': minor
3+
---
4+
5+
Update input types to allow omiting null inputs

codegen.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ const config: CodegenConfig = {
55
schema: 'https://core-api.uk.plain.com/graphql/v1/schema.graphql',
66
documents: './src/graphql/*/*.gql',
77
config: {
8-
avoidOptionals: true,
8+
avoidOptionals: {
9+
field: true,
10+
},
911
},
1012
generates: {
1113
'./src/graphql/types.ts': {

src/client.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
1+
import { VariablesOf } from '@graphql-typed-document-node/core';
12
import { Context } from './context';
23
import { PlainSDKError } from './error';
34
import {
5+
AddCustomerToCustomerGroupsDocument,
6+
CustomerByIdDocument,
47
CustomerGroupMembershipPartsFragment,
58
RemoveCustomerFromCustomerGroupsDocument,
6-
RemoveCustomerFromCustomerGroupsInput,
79
} from './graphql/types';
8-
import { AddCustomerToCustomerGroupsDocument } from './graphql/types';
9-
import { AddCustomerToCustomerGroupsInput } from './graphql/types';
1010
import {
1111
CreateIssueDocument,
12-
CreateIssueInput,
13-
CustomerByIdDocument,
14-
CustomerByIdQueryVariables,
1512
CustomerPartsFragment,
1613
IssuePartsFragment,
1714
TimelineEntryPartsFragment,
1815
UpsertCustomTimelineEntryDocument,
19-
UpsertCustomTimelineEntryInput,
2016
UpsertCustomerDocument,
21-
UpsertCustomerInput,
2217
UpsertResult,
2318
} from './graphql/types';
2419
import { request } from './request';
2520
import { Result } from './result';
2621

2722
type SDKResult<T> = Promise<Result<T, PlainSDKError>>;
2823

29-
function nonNullable<T>(x: T | null): T {
30-
if (x === null) {
24+
function nonNullable<T>(x: T | null | undefined): T {
25+
if (x === null || x === undefined) {
3126
throw new Error(`Expected value to be non nullable`);
3227
}
3328

@@ -74,12 +69,12 @@ export class PlainSDKClient {
7469
/**
7570
* If the customer is not found this will return null.
7671
*/
77-
async getCustomerById(args: CustomerByIdQueryVariables): SDKResult<CustomerPartsFragment | null> {
72+
async getCustomerById(
73+
variables: VariablesOf<typeof CustomerByIdDocument>
74+
): SDKResult<CustomerPartsFragment | null> {
7875
const res = await request(this.#ctx, {
7976
query: CustomerByIdDocument,
80-
variables: {
81-
customerId: args.customerId,
82-
},
77+
variables,
8378
});
8479

8580
return unwrapData(res, (q) => q.customer);
@@ -89,7 +84,9 @@ export class PlainSDKClient {
8984
* Allows you to create or update a customer. If you need to get the customer id
9085
* for a customer in Plain, this is typically your first step.
9186
*/
92-
async upsertCustomer(input: UpsertCustomerInput): SDKResult<CustomerPartsFragment | null> {
87+
async upsertCustomer(
88+
input: VariablesOf<typeof UpsertCustomerDocument>['input']
89+
): SDKResult<CustomerPartsFragment> {
9390
const res = await request(this.#ctx, {
9491
query: UpsertCustomerDocument,
9592
variables: {
@@ -104,7 +101,9 @@ export class PlainSDKClient {
104101
* Create an issue for a customer. If you want you can override the default issue priority
105102
* in your settings by specifying a priority manually here.
106103
*/
107-
async createIssue(input: CreateIssueInput): SDKResult<IssuePartsFragment> {
104+
async createIssue(
105+
input: VariablesOf<typeof CreateIssueDocument>['input']
106+
): SDKResult<IssuePartsFragment> {
108107
const res = await request(this.#ctx, {
109108
query: CreateIssueDocument,
110109
variables: {
@@ -119,7 +118,7 @@ export class PlainSDKClient {
119118
* Adds a customer to a customer groups.
120119
*/
121120
async addCustomerToCustomerGroups(
122-
input: AddCustomerToCustomerGroupsInput
121+
input: VariablesOf<typeof AddCustomerToCustomerGroupsDocument>['input']
123122
): SDKResult<CustomerGroupMembershipPartsFragment[]> {
124123
const res = await request(this.#ctx, {
125124
query: AddCustomerToCustomerGroupsDocument,
@@ -137,7 +136,7 @@ export class PlainSDKClient {
137136
* Remove a customer from customer groups.
138137
*/
139138
async removeCustomerFromCustomerGroups(
140-
input: RemoveCustomerFromCustomerGroupsInput
139+
input: VariablesOf<typeof RemoveCustomerFromCustomerGroupsDocument>['input']
141140
): SDKResult<null> {
142141
const res = await request(this.#ctx, {
143142
query: RemoveCustomerFromCustomerGroupsDocument,
@@ -155,7 +154,7 @@ export class PlainSDKClient {
155154
* This can be used to power custom contact forms, log events from your own systems and much more.
156155
*/
157156
async upsertCustomTimelineEntry(
158-
input: UpsertCustomTimelineEntryInput
157+
input: VariablesOf<typeof UpsertCustomTimelineEntryDocument>['input']
159158
): SDKResult<{ result: UpsertResult; timelineEntry: TimelineEntryPartsFragment }> {
160159
const res = await request(this.#ctx, {
161160
query: UpsertCustomTimelineEntryDocument,

src/examples/upsertCustomer.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* This example calls upsert customer. This file acts as kind of type 'test'
3+
* since breaking the API would result in this file not typechecking.
4+
*/
5+
6+
import { PlainSDKClient } from '../client';
7+
8+
export async function upsertCustomerExample() {
9+
const client = new PlainSDKClient({ apiKey: '' });
10+
11+
const res = await client.upsertCustomer({
12+
identifier: {
13+
customerId: '',
14+
},
15+
onCreate: {
16+
fullName: '',
17+
email: {
18+
email: '',
19+
isVerified: false,
20+
},
21+
},
22+
onUpdate: {},
23+
});
24+
25+
if (res.error) {
26+
console.error(res.error);
27+
throw new Error(res.error.message);
28+
}
29+
30+
console.log(`Created customer with ${res.data.id}`);
31+
}

0 commit comments

Comments
 (0)