Skip to content

Commit

Permalink
feat(subscribers) (#9)
Browse files Browse the repository at this point in the history
* fix lint

* copy-paste some tests

* finish subscribers
  • Loading branch information
HugoDF authored Apr 15, 2020
1 parent 321b0b9 commit 442c939
Show file tree
Hide file tree
Showing 3 changed files with 435 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import * as emails from './emails';
import * as images from './images';
import * as newsletters from './newsletters';
import * as scheduledEmails from './scheduled-emails';
// Import * as subscribers from './subscribers';
// import * as tags from './tags';
import * as subscribers from './subscribers';
// Import * as tags from './tags';
// import * as unsubscribers from './unsubscribers';

export default {
Expand All @@ -21,8 +21,8 @@ export default {
images,
newsletters,
ping,
scheduledEmails
// Subscribers,
// tags,
scheduledEmails,
subscribers
// Tags,
// unsubscribers
};
105 changes: 105 additions & 0 deletions src/subscribers.ts
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
});
}
Loading

0 comments on commit 442c939

Please sign in to comment.