generated from HugoDF/node-mit-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix lint * copy-paste some tests * finish subscribers
- Loading branch information
Showing
3 changed files
with
435 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */ | ||
import client, {VERBS, RESOURCES} from './lib/client'; | ||
import {validateNonEmptyObject, validatePresence} from './lib/validate'; | ||
|
||
interface SubscriberCreateFields { | ||
readonly email: string; | ||
readonly notes: string; | ||
readonly referrer_url: string; | ||
readonly tags: string[]; | ||
} | ||
|
||
type SubscriberType = 'regular' | 'unactivated' | 'unpaid' | 'removed'; | ||
|
||
interface SubscriberQueryFilters { | ||
readonly type?: SubscriberType; | ||
readonly email?: string; | ||
readonly tag?: string; | ||
} | ||
|
||
interface SubscriberRecord extends SubscriberCreateFields { | ||
readonly creation_date: string; | ||
readonly id: string; | ||
readonly metadata?: Record<any, any>; | ||
readonly secondary_id: number; | ||
readonly subscriber_type: SubscriberType; | ||
readonly source: string; | ||
readonly tags: string[]; | ||
readonly utm_campaign: string; | ||
readonly utm_medium: string; | ||
readonly utm_source: string; | ||
} | ||
|
||
type SubscriberList = SubscriberRecord[]; | ||
|
||
const REQUIRED_FIELDS = ['email']; | ||
|
||
export async function list( | ||
page = 1, | ||
query: SubscriberQueryFilters = {} | ||
): Promise<SubscriberList> { | ||
return client.request<SubscriberList>(VERBS.GET, RESOURCES.SUBSCRIBERS, { | ||
query: { | ||
...query, | ||
page | ||
} | ||
}); | ||
} | ||
|
||
export async function create(fields: SubscriberCreateFields): Promise<void> { | ||
validatePresence( | ||
fields, | ||
REQUIRED_FIELDS, | ||
'buttondown.subscribers.create() - email is required' | ||
); | ||
return client.request<void>(VERBS.POST, RESOURCES.SUBSCRIBERS, { | ||
payload: fields | ||
}); | ||
} | ||
|
||
export async function get(id: string): Promise<SubscriberRecord> { | ||
if (!id) { | ||
throw new Error('buttondown.subscribers.get() - id is required'); | ||
} | ||
|
||
return client.request<SubscriberRecord>(VERBS.GET, RESOURCES.SUBSCRIBERS, { | ||
resourcePath: id | ||
}); | ||
} | ||
|
||
export async function put( | ||
id: string, | ||
fields: SubscriberRecord | ||
): Promise<SubscriberRecord> { | ||
if (!id) { | ||
throw new Error('buttondown.subscribers.put() - id is required'); | ||
} | ||
|
||
validatePresence( | ||
fields, | ||
REQUIRED_FIELDS, | ||
'buttondown.subscribers.put() - email is required' | ||
); | ||
return client.request<SubscriberRecord>(VERBS.PUT, RESOURCES.SUBSCRIBERS, { | ||
resourcePath: id, | ||
payload: fields | ||
}); | ||
} | ||
|
||
export async function patch( | ||
id: string, | ||
fields: Partial<SubscriberRecord> | ||
): Promise<SubscriberRecord> { | ||
if (!id) { | ||
throw new Error('buttondown.subscribers.patch() - id is required'); | ||
} | ||
|
||
validateNonEmptyObject( | ||
fields, | ||
"buttondown.subscribers.patch() - can't patch subscriber to {}" | ||
); | ||
return client.request<SubscriberRecord>(VERBS.PATCH, RESOURCES.SUBSCRIBERS, { | ||
resourcePath: id, | ||
payload: fields | ||
}); | ||
} |
Oops, something went wrong.