@@ -8,6 +8,8 @@ import * as marked from 'marked';
8
8
import * as yaml from 'js-yaml' ;
9
9
import fetch from 'node-fetch' ;
10
10
11
+ import * as SG from './sendgrid' ;
12
+
11
13
const readFile = promisify ( fs . readFile ) ;
12
14
const writeFile = promisify ( fs . writeFile ) ;
13
15
const readdir = promisify ( fs . readdir ) ;
@@ -45,45 +47,6 @@ async function postToSlack(slackUrl: string, url: string) {
45
47
} ) ;
46
48
}
47
49
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
-
87
50
type Options = {
88
51
apiKey ?: string ,
89
52
filePath : string ,
@@ -95,6 +58,7 @@ type Options = {
95
58
siteYaml ?: string ,
96
59
subject ?: string ,
97
60
slackUrl ?: string ,
61
+ index ?: SG . SingleSendIndex ,
98
62
} ;
99
63
100
64
async function loadTemplate ( path ?: string , options ?: CompileOptions ) {
@@ -192,6 +156,12 @@ async function render(opts: Options) {
192
156
} ;
193
157
}
194
158
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
+
195
165
function getSendDate ( c : TemplateContext ) {
196
166
let date = c . post . date ;
197
167
if ( date . getTime ( ) <= Date . now ( ) ) {
@@ -213,13 +183,16 @@ async function run(options: Options) {
213
183
await writeFile ( options . output , text ) ;
214
184
} else if ( options . apiKey ) {
215
185
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 ( {
217
188
html : text ,
218
189
listId : options . listId ,
219
190
suppressionGroup : options . suppressionGroupId ,
220
191
token : options . apiKey ,
221
192
sendAt,
222
193
subject : ( options . subject ?? '%s' ) . replace ( '%s' , context . post . title ) ,
194
+ categories : [ 'newsletter' ] ,
195
+ id,
223
196
} ) ;
224
197
225
198
const url = response . headers . get ( 'location' ) ;
@@ -271,10 +244,14 @@ async function runAll(options: RunOptions) {
271
244
return ;
272
245
}
273
246
247
+ const index = await SG . indexSingleSends ( { token : options . apiKey } ) ;
248
+
274
249
for ( const post of posts ) {
250
+
275
251
const result = await run ( {
276
252
...options ,
277
- filePath : post
253
+ filePath : post ,
254
+ index,
278
255
} ) ;
279
256
280
257
if ( result ?. url && options . slackUrl ) {
0 commit comments