@@ -8,6 +8,8 @@ import * as marked from 'marked';
88import * as yaml from 'js-yaml' ;
99import fetch from 'node-fetch' ;
1010
11+ import * as SG from './sendgrid' ;
12+
1113const readFile = promisify ( fs . readFile ) ;
1214const writeFile = promisify ( fs . writeFile ) ;
1315const readdir = promisify ( fs . readdir ) ;
@@ -45,45 +47,6 @@ async function postToSlack(slackUrl: string, url: string) {
4547 } ) ;
4648}
4749
48- const API_BASE = 'https://api.sendgrid.com/v3' ;
49- type SingleSendParams = {
50- html : string ,
51- listId : string ,
52- suppressionGroup : number ,
53- token : string ,
54- sendAt ?: Date ,
55- subject : string ,
56- } ;
57- async function singleSend ( params : SingleSendParams ) {
58- return await fetch ( `${ API_BASE } /marketing/singlesends` , {
59- method : 'POST' ,
60- headers : {
61- 'Authorization' : `Bearer ${ params . token } ` ,
62- 'Content-type' : 'application/json' ,
63- } ,
64- body : JSON . stringify ( {
65- name : `Newsletter: ${ params . subject } ` ,
66- send_at : params . sendAt ?. toISOString ( ) ,
67- send_to : {
68- list_ids : [ params . listId ]
69- } ,
70- email_config : {
71- subject : params . subject ,
72- html_content : params . html ,
73- suppression_group_id : params . suppressionGroup
74- }
75- } )
76- } ) ;
77- }
78-
79- type GetSingleSendsParams = {
80-
81- } ;
82-
83- async function getSingleSends ( params : GetSingleSendsParams ) {
84-
85- }
86-
8750type Options = {
8851 apiKey ?: string ,
8952 filePath : string ,
@@ -95,6 +58,7 @@ type Options = {
9558 siteYaml ?: string ,
9659 subject ?: string ,
9760 slackUrl ?: string ,
61+ index ?: SG . SingleSendIndex ,
9862} ;
9963
10064async function loadTemplate ( path ?: string , options ?: CompileOptions ) {
@@ -192,6 +156,12 @@ async function render(opts: Options) {
192156 } ;
193157}
194158
159+ const dateStr = ( d : Date ) => ( ( d . toISOString ( ) ) . split ( 'T' , 1 ) [ 0 ] ) ;
160+
161+ const floorDate = ( d : Date ) => ( new Date ( d . getFullYear ( ) ,
162+ d . getMonth ( ) ,
163+ d . getDate ( ) ) ) ;
164+
195165function getSendDate ( c : TemplateContext ) {
196166 let date = c . post . date ;
197167 if ( date . getTime ( ) <= Date . now ( ) ) {
@@ -213,13 +183,16 @@ async function run(options: Options) {
213183 await writeFile ( options . output , text ) ;
214184 } else if ( options . apiKey ) {
215185 const sendAt = getSendDate ( context ) ;
216- const response = await singleSend ( {
186+ const id = options . index ?. byDate [ dateStr ( context . post . date ) ] [ 0 ] ;
187+ const response = await SG . singleSend ( {
217188 html : text ,
218189 listId : options . listId ,
219190 suppressionGroup : options . suppressionGroupId ,
220191 token : options . apiKey ,
221192 sendAt,
222193 subject : ( options . subject ?? '%s' ) . replace ( '%s' , context . post . title ) ,
194+ categories : [ 'newsletter' ] ,
195+ id,
223196 } ) ;
224197
225198 const url = response . headers . get ( 'location' ) ;
@@ -271,10 +244,14 @@ async function runAll(options: RunOptions) {
271244 return ;
272245 }
273246
247+ const index = await SG . indexSingleSends ( { token : options . apiKey } ) ;
248+
274249 for ( const post of posts ) {
250+
275251 const result = await run ( {
276252 ...options ,
277- filePath : post
253+ filePath : post ,
254+ index,
278255 } ) ;
279256
280257 if ( result ?. url && options . slackUrl ) {
0 commit comments