@geekmidas/cli 9.2.1 → 9.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +67 -0
- package/README.md +1 -1
- package/dist/index.cjs +75 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +75 -25
- package/dist/index.mjs.map +1 -1
- package/dist/{openapi-BlIiUahn.mjs → openapi-Dm1_sCxe.mjs} +4 -8
- package/dist/openapi-Dm1_sCxe.mjs.map +1 -0
- package/dist/{openapi-C-YgZHyd.cjs → openapi-ayEknaaM.cjs} +4 -8
- package/dist/openapi-ayEknaaM.cjs.map +1 -0
- package/dist/openapi.cjs +1 -1
- package/dist/openapi.mjs +1 -1
- package/docs/manifest-refactor-design.md +1 -1
- package/examples/cron-example.ts +1 -1
- package/package.json +9 -9
- package/src/__tests__/test-helpers.ts +4 -4
- package/src/build/__tests__/index-new.spec.ts +3 -3
- package/src/build/__tests__/manifests.spec.ts +2 -2
- package/src/generators/CronGenerator.ts +77 -2
- package/src/generators/EndpointGenerator.ts +0 -13
- package/src/generators/FunctionGenerator.ts +0 -1
- package/src/generators/__tests__/CronGenerator.spec.ts +88 -30
- package/src/generators/__tests__/EndpointGenerator.spec.ts +2 -80
- package/src/generators/__tests__/FunctionGenerator.spec.ts +0 -20
- package/src/init/versions.ts +19 -19
- package/dist/openapi-BlIiUahn.mjs.map +0 -1
- package/dist/openapi-C-YgZHyd.cjs.map +0 -1
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { mkdir, writeFile } from 'node:fs/promises';
|
|
2
2
|
import { dirname, join, relative } from 'node:path';
|
|
3
3
|
import { Cron } from '@geekmidas/constructs/crons';
|
|
4
|
+
import {
|
|
5
|
+
type CronField,
|
|
6
|
+
cronPeriod,
|
|
7
|
+
InvalidCronExpressionError,
|
|
8
|
+
toAwsCron,
|
|
9
|
+
} from '@geekmidas/manifest';
|
|
4
10
|
import type { BuildContext } from '../build/types';
|
|
5
11
|
import type { CronInfo } from '../types';
|
|
6
12
|
import {
|
|
@@ -24,6 +30,8 @@ export class CronGenerator extends ConstructGenerator<
|
|
|
24
30
|
const provider = options?.provider || 'aws-lambda';
|
|
25
31
|
const logger = console;
|
|
26
32
|
const cronInfos: CronInfo[] = [];
|
|
33
|
+
// Every provider validates, so `gkm dev` fails like `gkm build` does.
|
|
34
|
+
const schedules = resolveSchedules(constructs, logger);
|
|
27
35
|
|
|
28
36
|
if (constructs.length === 0 || provider !== 'aws-lambda') {
|
|
29
37
|
return cronInfos;
|
|
@@ -42,19 +50,22 @@ export class CronGenerator extends ConstructGenerator<
|
|
|
42
50
|
context,
|
|
43
51
|
);
|
|
44
52
|
|
|
53
|
+
const schedule = schedules.get(key) as string;
|
|
54
|
+
const uptime = construct.getUptime();
|
|
55
|
+
|
|
45
56
|
cronInfos.push({
|
|
46
57
|
name: key,
|
|
47
58
|
handler: relative(process.cwd(), handlerFile).replace(
|
|
48
59
|
/\.ts$/,
|
|
49
60
|
'.handler',
|
|
50
61
|
),
|
|
51
|
-
schedule
|
|
62
|
+
schedule,
|
|
52
63
|
timeout: construct.timeout,
|
|
53
64
|
memorySize: construct.memorySize,
|
|
54
65
|
environment: await construct.getEnvironment({
|
|
55
66
|
markOptional: context.markOptional,
|
|
56
67
|
}),
|
|
57
|
-
|
|
68
|
+
...(uptime && { uptime: { ...uptime, period: cronPeriod(schedule) } }),
|
|
58
69
|
});
|
|
59
70
|
|
|
60
71
|
logger.log(`Generated cron handler: ${key}`);
|
|
@@ -104,3 +115,67 @@ export const handler = adapter.handler;
|
|
|
104
115
|
return handlerPath;
|
|
105
116
|
}
|
|
106
117
|
}
|
|
118
|
+
|
|
119
|
+
/** A cron whose schedule failed validation. */
|
|
120
|
+
export interface InvalidCronSchedule {
|
|
121
|
+
cron: string;
|
|
122
|
+
schedule: string;
|
|
123
|
+
/** Absent when the schedule isn't `cron(…)` with 5 or 6 fields. */
|
|
124
|
+
field?: CronField;
|
|
125
|
+
token: string;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** One or more crons have an invalid schedule; all of them are listed. */
|
|
129
|
+
export class InvalidCronSchedulesError extends Error {
|
|
130
|
+
constructor(readonly crons: InvalidCronSchedule[]) {
|
|
131
|
+
super('Invalid cron schedules', { cause: { crons } });
|
|
132
|
+
this.name = 'InvalidCronSchedulesError';
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Hourly, when a cron sets no schedule. */
|
|
137
|
+
const DEFAULT_SCHEDULE = 'cron(0 * * * *)';
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Each cron's schedule in the AWS form the manifest records, keyed by cron.
|
|
141
|
+
* Every schedule is validated, since a cast or JavaScript caller gets past
|
|
142
|
+
* the types, and all invalid ones are reported together.
|
|
143
|
+
*
|
|
144
|
+
* @throws {InvalidCronSchedulesError}
|
|
145
|
+
*/
|
|
146
|
+
function resolveSchedules(
|
|
147
|
+
constructs: GeneratedConstruct<
|
|
148
|
+
Cron<any, any, any, any, any, any, any, any>
|
|
149
|
+
>[],
|
|
150
|
+
logger: Pick<Console, 'warn'>,
|
|
151
|
+
): Map<string, string> {
|
|
152
|
+
const schedules = new Map<string, string>();
|
|
153
|
+
const invalid: InvalidCronSchedule[] = [];
|
|
154
|
+
|
|
155
|
+
for (const { key, construct } of constructs) {
|
|
156
|
+
const schedule = construct.schedule ?? DEFAULT_SCHEDULE;
|
|
157
|
+
try {
|
|
158
|
+
const { expression, warnings } = toAwsCron(schedule);
|
|
159
|
+
for (const warning of warnings) {
|
|
160
|
+
logger.warn('Cron schedule converted to AWS approximately', {
|
|
161
|
+
cron: key,
|
|
162
|
+
schedule,
|
|
163
|
+
expression,
|
|
164
|
+
...warning,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
schedules.set(key, expression);
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (!(error instanceof InvalidCronExpressionError)) throw error;
|
|
170
|
+
invalid.push({
|
|
171
|
+
cron: key,
|
|
172
|
+
schedule,
|
|
173
|
+
field: error.field,
|
|
174
|
+
token: error.token,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (invalid.length > 0) throw new InvalidCronSchedulesError(invalid);
|
|
180
|
+
return schedules;
|
|
181
|
+
}
|
|
@@ -95,22 +95,11 @@ export class EndpointGenerator extends ConstructGenerator<
|
|
|
95
95
|
await this.generateEndpointsFile(outputDir, constructs, context);
|
|
96
96
|
const appFile = await this.generateAppFile(outputDir, context);
|
|
97
97
|
|
|
98
|
-
// One process serves every endpoint, so its monitors are the distinct
|
|
99
|
-
// ones across them: endpoints reading the same variable share its URL.
|
|
100
|
-
const monitoring = [
|
|
101
|
-
...new Map(
|
|
102
|
-
constructs
|
|
103
|
-
.flatMap(({ construct }) => construct.getMonitoring() ?? [])
|
|
104
|
-
.map((monitor) => [JSON.stringify(monitor), monitor]),
|
|
105
|
-
).values(),
|
|
106
|
-
];
|
|
107
|
-
|
|
108
98
|
routes.push({
|
|
109
99
|
path: '*',
|
|
110
100
|
method: 'ALL',
|
|
111
101
|
handler: relative(process.cwd(), appFile),
|
|
112
102
|
authorizer: 'none',
|
|
113
|
-
...(monitoring.length ? { monitoring } : {}),
|
|
114
103
|
});
|
|
115
104
|
|
|
116
105
|
logger.log(
|
|
@@ -145,7 +134,6 @@ export class EndpointGenerator extends ConstructGenerator<
|
|
|
145
134
|
markOptional: context.markOptional,
|
|
146
135
|
}),
|
|
147
136
|
authorizer: construct.authorizer?.name ?? 'none',
|
|
148
|
-
monitoring: construct.getMonitoring(),
|
|
149
137
|
};
|
|
150
138
|
|
|
151
139
|
routes.push(routeInfo);
|
|
@@ -178,7 +166,6 @@ export class EndpointGenerator extends ConstructGenerator<
|
|
|
178
166
|
markOptional: context.markOptional,
|
|
179
167
|
}),
|
|
180
168
|
authorizer: construct.authorizer?.name ?? 'none',
|
|
181
|
-
monitoring: construct.getMonitoring(),
|
|
182
169
|
};
|
|
183
170
|
|
|
184
171
|
routes.push(routeInfo);
|
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
createTempDir,
|
|
16
16
|
createTestCron,
|
|
17
17
|
} from '../../__tests__/test-helpers';
|
|
18
|
-
import { CronGenerator } from '../CronGenerator';
|
|
18
|
+
import { CronGenerator, InvalidCronSchedulesError } from '../CronGenerator';
|
|
19
19
|
import type { GeneratedConstruct } from '../Generator';
|
|
20
20
|
|
|
21
21
|
describe('CronGenerator', () => {
|
|
@@ -37,7 +37,7 @@ describe('CronGenerator', () => {
|
|
|
37
37
|
|
|
38
38
|
describe('isConstruct', () => {
|
|
39
39
|
it('should identify valid crons', () => {
|
|
40
|
-
const testCron = createTestCron('
|
|
40
|
+
const testCron = createTestCron('cron(0 * * * *)', 30);
|
|
41
41
|
expect(generator.isConstruct(testCron)).toBe(true);
|
|
42
42
|
});
|
|
43
43
|
|
|
@@ -51,7 +51,7 @@ describe('CronGenerator', () => {
|
|
|
51
51
|
describe('build', () => {
|
|
52
52
|
const _createMockCronConstruct = (
|
|
53
53
|
key: string,
|
|
54
|
-
schedule: ScheduleExpression = '
|
|
54
|
+
schedule: ScheduleExpression = 'cron(0 * * * *)',
|
|
55
55
|
timeout: number = 30,
|
|
56
56
|
): GeneratedConstruct<Cron<any, any, any, any>> => ({
|
|
57
57
|
key,
|
|
@@ -74,13 +74,13 @@ describe('CronGenerator', () => {
|
|
|
74
74
|
cronsDir,
|
|
75
75
|
'dailyReport.ts',
|
|
76
76
|
'dailyReport',
|
|
77
|
-
'
|
|
77
|
+
'cron(0 0 * * *)',
|
|
78
78
|
),
|
|
79
79
|
createMockCronFile(
|
|
80
80
|
cronsDir,
|
|
81
81
|
'hourlyCleanup.ts',
|
|
82
82
|
'hourlyCleanup',
|
|
83
|
-
'
|
|
83
|
+
'cron(0 * * * *)',
|
|
84
84
|
),
|
|
85
85
|
createMockCronFile(
|
|
86
86
|
cronsDir,
|
|
@@ -109,12 +109,12 @@ describe('CronGenerator', () => {
|
|
|
109
109
|
expect(dailyReport).toMatchObject({
|
|
110
110
|
name: 'dailyReport',
|
|
111
111
|
handler: expect.stringContaining('crons/dailyReport.handler'),
|
|
112
|
-
schedule: '
|
|
112
|
+
schedule: 'cron(0 0 * * ? *)',
|
|
113
113
|
});
|
|
114
114
|
expect(hourlyCleanup).toMatchObject({
|
|
115
115
|
name: 'hourlyCleanup',
|
|
116
116
|
handler: expect.stringContaining('crons/hourlyCleanup.handler'),
|
|
117
|
-
schedule: '
|
|
117
|
+
schedule: 'cron(0 * * * ? *)',
|
|
118
118
|
});
|
|
119
119
|
expect(weeklyBackup).toMatchObject({
|
|
120
120
|
name: 'weeklyBackup',
|
|
@@ -150,10 +150,14 @@ describe('CronGenerator', () => {
|
|
|
150
150
|
expect(hourlyCleanupContent).toContain('import { hourlyCleanup }');
|
|
151
151
|
});
|
|
152
152
|
|
|
153
|
-
it(
|
|
153
|
+
it.each([
|
|
154
|
+
['cron(0 8 * * ? *)', 86400],
|
|
155
|
+
['cron(*/15 * * * *)', 900],
|
|
156
|
+
['cron(0 9 * * MON-FRI)', 259200],
|
|
157
|
+
])('records %s’s uptime with a period of %i seconds', async (schedule, period) => {
|
|
154
158
|
const monitored = new CronBuilder()
|
|
155
|
-
.schedule(
|
|
156
|
-
.
|
|
159
|
+
.schedule(schedule as ScheduleExpression)
|
|
160
|
+
.uptime(heartbeat.betterStack('MONITORING_URL'))
|
|
157
161
|
.handle(async () => ({}));
|
|
158
162
|
const plain = createTestCron();
|
|
159
163
|
|
|
@@ -183,15 +187,17 @@ describe('CronGenerator', () => {
|
|
|
183
187
|
{ provider: 'aws-lambda' },
|
|
184
188
|
);
|
|
185
189
|
|
|
186
|
-
expect(cronInfos[0].
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
+
expect(cronInfos[0].uptime).toEqual({
|
|
191
|
+
type: 'heartbeat',
|
|
192
|
+
provider: 'betterStack',
|
|
193
|
+
env: 'MONITORING_URL',
|
|
194
|
+
period,
|
|
195
|
+
});
|
|
196
|
+
expect(cronInfos[1].uptime).toBeUndefined();
|
|
190
197
|
});
|
|
191
198
|
|
|
192
199
|
it('should use default schedule when none provided', async () => {
|
|
193
|
-
|
|
194
|
-
const cronWithDefaultSchedule = createTestCron(undefined, 30);
|
|
200
|
+
const cronWithDefaultSchedule = createTestCron(null, 30);
|
|
195
201
|
|
|
196
202
|
const construct: GeneratedConstruct<Cron<any, any, any, any>> = {
|
|
197
203
|
key: 'defaultScheduleCron',
|
|
@@ -210,8 +216,18 @@ describe('CronGenerator', () => {
|
|
|
210
216
|
{ provider: 'aws-lambda' },
|
|
211
217
|
);
|
|
212
218
|
|
|
213
|
-
|
|
214
|
-
|
|
219
|
+
expect(cronInfos[0].schedule).toBe('cron(0 * * * ? *)');
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it('keeps day-of-week when a Unix schedule restricts both day fields', async () => {
|
|
223
|
+
const cronInfos = await generator.build(
|
|
224
|
+
context,
|
|
225
|
+
[_createMockCronConstruct('monthly', 'cron(0 9 1 * MON)')],
|
|
226
|
+
outputDir,
|
|
227
|
+
{ provider: 'aws-lambda' },
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
expect(cronInfos[0].schedule).toBe('cron(0 9 ? * MON *)');
|
|
215
231
|
});
|
|
216
232
|
|
|
217
233
|
itWithDir(
|
|
@@ -225,7 +241,7 @@ describe('CronGenerator', () => {
|
|
|
225
241
|
nestedDir,
|
|
226
242
|
'deepCron.ts',
|
|
227
243
|
'deepCron',
|
|
228
|
-
'
|
|
244
|
+
'cron(0 0 * * *)',
|
|
229
245
|
);
|
|
230
246
|
|
|
231
247
|
const constructs = await generator.load(
|
|
@@ -259,7 +275,7 @@ describe('CronGenerator', () => {
|
|
|
259
275
|
cronsDir,
|
|
260
276
|
'testCron.ts',
|
|
261
277
|
'testCron',
|
|
262
|
-
'
|
|
278
|
+
'cron(0 * * * *)',
|
|
263
279
|
);
|
|
264
280
|
|
|
265
281
|
const constructs = await generator.load('**/crons/*.ts', dir);
|
|
@@ -286,7 +302,7 @@ describe('CronGenerator', () => {
|
|
|
286
302
|
cronsDir,
|
|
287
303
|
'testCron.ts',
|
|
288
304
|
'testCron',
|
|
289
|
-
'
|
|
305
|
+
'cron(0 * * * *)',
|
|
290
306
|
);
|
|
291
307
|
|
|
292
308
|
const constructs = await generator.load('**/crons/*.ts', dir);
|
|
@@ -313,7 +329,7 @@ describe('CronGenerator', () => {
|
|
|
313
329
|
cronsDir,
|
|
314
330
|
'testCron.ts',
|
|
315
331
|
'testCron',
|
|
316
|
-
'
|
|
332
|
+
'cron(0 * * * *)',
|
|
317
333
|
);
|
|
318
334
|
|
|
319
335
|
const constructs = await generator.load('**/crons/*.ts', dir);
|
|
@@ -330,6 +346,48 @@ describe('CronGenerator', () => {
|
|
|
330
346
|
);
|
|
331
347
|
});
|
|
332
348
|
|
|
349
|
+
it.each([
|
|
350
|
+
'aws-lambda',
|
|
351
|
+
'server',
|
|
352
|
+
] as const)('fails the %s build listing every invalid schedule', async (provider) => {
|
|
353
|
+
const build = generator.build(
|
|
354
|
+
context,
|
|
355
|
+
[
|
|
356
|
+
_createMockCronConstruct('valid', 'cron(0 8 * * *)'),
|
|
357
|
+
_createMockCronConstruct(
|
|
358
|
+
'badHour',
|
|
359
|
+
'cron(0 25 * * *)' as ScheduleExpression,
|
|
360
|
+
),
|
|
361
|
+
_createMockCronConstruct('rate', 'rate(1 day)' as ScheduleExpression),
|
|
362
|
+
],
|
|
363
|
+
outputDir,
|
|
364
|
+
{ provider },
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
await expect(build).rejects.toThrow(InvalidCronSchedulesError);
|
|
368
|
+
await expect(build).rejects.toThrow(
|
|
369
|
+
expect.objectContaining({
|
|
370
|
+
message: 'Invalid cron schedules',
|
|
371
|
+
cause: {
|
|
372
|
+
crons: [
|
|
373
|
+
{
|
|
374
|
+
cron: 'badHour',
|
|
375
|
+
schedule: 'cron(0 25 * * *)',
|
|
376
|
+
field: 'hour',
|
|
377
|
+
token: '25',
|
|
378
|
+
},
|
|
379
|
+
{
|
|
380
|
+
cron: 'rate',
|
|
381
|
+
schedule: 'rate(1 day)',
|
|
382
|
+
field: undefined,
|
|
383
|
+
token: 'rate(1 day)',
|
|
384
|
+
},
|
|
385
|
+
],
|
|
386
|
+
},
|
|
387
|
+
}),
|
|
388
|
+
);
|
|
389
|
+
});
|
|
390
|
+
|
|
333
391
|
it('should return empty array for empty constructs', async () => {
|
|
334
392
|
const cronInfos = await generator.build(context, [], outputDir, {
|
|
335
393
|
provider: 'aws-lambda',
|
|
@@ -348,7 +406,7 @@ describe('CronGenerator', () => {
|
|
|
348
406
|
cronsDir,
|
|
349
407
|
'defaultCron.ts',
|
|
350
408
|
'defaultCron',
|
|
351
|
-
'
|
|
409
|
+
'cron(0 * * * *)',
|
|
352
410
|
);
|
|
353
411
|
|
|
354
412
|
const constructs = await generator.load('**/crons/*.ts', dir);
|
|
@@ -375,7 +433,7 @@ describe('CronGenerator', () => {
|
|
|
375
433
|
cronsDir,
|
|
376
434
|
'rateCron.ts',
|
|
377
435
|
'rateCron',
|
|
378
|
-
'
|
|
436
|
+
'cron(*/5 * * * *)',
|
|
379
437
|
),
|
|
380
438
|
createMockCronFile(
|
|
381
439
|
cronsDir,
|
|
@@ -387,7 +445,7 @@ describe('CronGenerator', () => {
|
|
|
387
445
|
cronsDir,
|
|
388
446
|
'dailyRate.ts',
|
|
389
447
|
'dailyRate',
|
|
390
|
-
'
|
|
448
|
+
'cron(0 0 * * *)',
|
|
391
449
|
),
|
|
392
450
|
]);
|
|
393
451
|
|
|
@@ -402,9 +460,9 @@ describe('CronGenerator', () => {
|
|
|
402
460
|
const cronExpression = cronInfos.find((c) => c.name === 'cronExpression');
|
|
403
461
|
const dailyRate = cronInfos.find((c) => c.name === 'dailyRate');
|
|
404
462
|
|
|
405
|
-
expect(rateCron?.schedule).toBe('
|
|
463
|
+
expect(rateCron?.schedule).toBe('cron(*/5 * * * ? *)');
|
|
406
464
|
expect(cronExpression?.schedule).toBe('cron(0 12 * * ? *)');
|
|
407
|
-
expect(dailyRate?.schedule).toBe('
|
|
465
|
+
expect(dailyRate?.schedule).toBe('cron(0 0 * * ? *)');
|
|
408
466
|
});
|
|
409
467
|
|
|
410
468
|
itWithDir(
|
|
@@ -419,13 +477,13 @@ describe('CronGenerator', () => {
|
|
|
419
477
|
cronsDir,
|
|
420
478
|
'quickCron.ts',
|
|
421
479
|
'quickCron',
|
|
422
|
-
'
|
|
480
|
+
'cron(* * * * *)',
|
|
423
481
|
),
|
|
424
482
|
createMockCronFile(
|
|
425
483
|
cronsDir,
|
|
426
484
|
'slowCron.ts',
|
|
427
485
|
'slowCron',
|
|
428
|
-
'
|
|
486
|
+
'cron(0 * * * *)',
|
|
429
487
|
),
|
|
430
488
|
]);
|
|
431
489
|
|
|
@@ -467,7 +525,7 @@ describe('CronGenerator', () => {
|
|
|
467
525
|
cronsDir,
|
|
468
526
|
'customCron.ts',
|
|
469
527
|
'customCron',
|
|
470
|
-
'
|
|
528
|
+
'cron(0 * * * *)',
|
|
471
529
|
);
|
|
472
530
|
|
|
473
531
|
const constructs = await generator.load('**/crons/*.ts', dir);
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import { mkdir,
|
|
2
|
-
import { tmpdir } from 'node:os';
|
|
1
|
+
import { mkdir, readFile } from 'node:fs/promises';
|
|
3
2
|
import { join } from 'node:path';
|
|
4
3
|
import type { HttpMethod } from '@geekmidas/constructs';
|
|
5
|
-
import {
|
|
6
|
-
import { heartbeat } from '@geekmidas/constructs/monitors';
|
|
4
|
+
import type { Endpoint } from '@geekmidas/constructs/endpoints';
|
|
7
5
|
import { itWithDir } from '@geekmidas/testkit/os';
|
|
8
6
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
9
7
|
import {
|
|
@@ -53,82 +51,6 @@ describe('EndpointGenerator', () => {
|
|
|
53
51
|
},
|
|
54
52
|
});
|
|
55
53
|
|
|
56
|
-
describe('monitoring', () => {
|
|
57
|
-
const MONITOR = {
|
|
58
|
-
type: 'heartbeat',
|
|
59
|
-
provider: 'betterStack',
|
|
60
|
-
env: 'MONITORING_URL',
|
|
61
|
-
};
|
|
62
|
-
let dir: string;
|
|
63
|
-
|
|
64
|
-
beforeEach(async () => {
|
|
65
|
-
dir = await mkdtemp(join(tmpdir(), 'endpoint-monitoring-'));
|
|
66
|
-
await mkdir(join(dir, 'output'), { recursive: true });
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
afterEach(async () => {
|
|
70
|
-
await rm(dir, { recursive: true, force: true });
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
const monitoredAt = (key: string, path: string) => {
|
|
74
|
-
const construct = createTestEndpointConstruct(key, path, 'GET', dir);
|
|
75
|
-
construct.construct = e
|
|
76
|
-
.monitor(heartbeat.betterStack('MONITORING_URL'))
|
|
77
|
-
.get(path)
|
|
78
|
-
.handle(async () => ({}));
|
|
79
|
-
return construct;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
it.each([
|
|
83
|
-
'aws-lambda',
|
|
84
|
-
'aws-apigatewayv2',
|
|
85
|
-
] as const)('records each route’s monitors (%s)', async (provider) => {
|
|
86
|
-
const routes = await generator.build(
|
|
87
|
-
context,
|
|
88
|
-
[
|
|
89
|
-
monitoredAt('monitored', '/monitored'),
|
|
90
|
-
createTestEndpointConstruct('plain', '/plain', 'GET', dir),
|
|
91
|
-
],
|
|
92
|
-
join(dir, 'output'),
|
|
93
|
-
{ provider },
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
expect(routes.find((r) => r.path === '/monitored')?.monitoring).toEqual([
|
|
97
|
-
MONITOR,
|
|
98
|
-
]);
|
|
99
|
-
expect(
|
|
100
|
-
routes.find((r) => r.path === '/plain')?.monitoring,
|
|
101
|
-
).toBeUndefined();
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it('records the distinct monitors across endpoints on the server route', async () => {
|
|
105
|
-
const routes = await generator.build(
|
|
106
|
-
context,
|
|
107
|
-
[
|
|
108
|
-
monitoredAt('first', '/first'),
|
|
109
|
-
monitoredAt('second', '/second'),
|
|
110
|
-
createTestEndpointConstruct('plain', '/plain', 'GET', dir),
|
|
111
|
-
],
|
|
112
|
-
join(dir, 'output'),
|
|
113
|
-
{ provider: 'server' },
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
expect(routes).toHaveLength(1);
|
|
117
|
-
expect(routes[0].monitoring).toEqual([MONITOR]);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('records no monitoring on a server whose endpoints have none', async () => {
|
|
121
|
-
const routes = await generator.build(
|
|
122
|
-
context,
|
|
123
|
-
[createTestEndpointConstruct('plain', '/plain', 'GET', dir)],
|
|
124
|
-
join(dir, 'output'),
|
|
125
|
-
{ provider: 'server' },
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
expect(routes[0].monitoring).toBeUndefined();
|
|
129
|
-
});
|
|
130
|
-
});
|
|
131
|
-
|
|
132
54
|
itWithDir('should generate a single server app file', async ({ dir }) => {
|
|
133
55
|
const outputDir = join(dir, 'output');
|
|
134
56
|
const routesDir = join(dir, 'routes');
|
|
@@ -2,7 +2,6 @@ import { readFile } from 'node:fs/promises';
|
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import type { Function } from '@geekmidas/constructs/functions';
|
|
4
4
|
import { f } from '@geekmidas/constructs/functions';
|
|
5
|
-
import { heartbeat } from '@geekmidas/constructs/monitors';
|
|
6
5
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
7
6
|
import { z } from 'zod';
|
|
8
7
|
import {
|
|
@@ -121,25 +120,6 @@ describe('FunctionGenerator', () => {
|
|
|
121
120
|
expect(sendEmailContent).toContain('import { sendEmail }');
|
|
122
121
|
});
|
|
123
122
|
|
|
124
|
-
it('records the function’s monitors in its manifest entry', async () => {
|
|
125
|
-
const monitored = createTestFunctionConstruct('monitored');
|
|
126
|
-
monitored.construct = f
|
|
127
|
-
.monitor(heartbeat.betterStack('MONITORING_URL'))
|
|
128
|
-
.handle(async () => ({}));
|
|
129
|
-
|
|
130
|
-
const functionInfos = await generator.build(
|
|
131
|
-
context,
|
|
132
|
-
[monitored, createTestFunctionConstruct('plain')],
|
|
133
|
-
outputDir,
|
|
134
|
-
{ provider: 'aws-lambda' },
|
|
135
|
-
);
|
|
136
|
-
|
|
137
|
-
expect(functionInfos[0].monitoring).toEqual([
|
|
138
|
-
{ type: 'heartbeat', provider: 'betterStack', env: 'MONITORING_URL' },
|
|
139
|
-
]);
|
|
140
|
-
expect(functionInfos[1].monitoring).toBeUndefined();
|
|
141
|
-
});
|
|
142
|
-
|
|
143
123
|
it('should generate correct relative import paths', async () => {
|
|
144
124
|
const construct: GeneratedConstruct<Function<any, any, any, any>> = {
|
|
145
125
|
key: 'deepFunction',
|
package/src/init/versions.ts
CHANGED
|
@@ -27,25 +27,25 @@ export const CLI_VERSION = `~${pkg.version}`;
|
|
|
27
27
|
* Run: pnpm --filter @geekmidas/cli sync-versions
|
|
28
28
|
*/
|
|
29
29
|
export const GEEKMIDAS_VERSIONS = {
|
|
30
|
-
'@geekmidas/audit': '~9.
|
|
31
|
-
'@geekmidas/auth': '~9.
|
|
32
|
-
'@geekmidas/cache': '~9.
|
|
33
|
-
'@geekmidas/client': '~9.
|
|
34
|
-
'@geekmidas/cloud': '~9.
|
|
35
|
-
'@geekmidas/constructs': '~9.
|
|
36
|
-
'@geekmidas/db': '~9.
|
|
37
|
-
'@geekmidas/emailkit': '~9.
|
|
38
|
-
'@geekmidas/envkit': '~9.
|
|
39
|
-
'@geekmidas/errors': '~9.
|
|
40
|
-
'@geekmidas/events': '~9.
|
|
41
|
-
'@geekmidas/logger': '~9.
|
|
42
|
-
'@geekmidas/rate-limit': '~9.
|
|
43
|
-
'@geekmidas/schema': '~9.
|
|
44
|
-
'@geekmidas/services': '~9.
|
|
45
|
-
'@geekmidas/storage': '~9.
|
|
46
|
-
'@geekmidas/studio': '~9.
|
|
47
|
-
'@geekmidas/telescope': '~9.
|
|
48
|
-
'@geekmidas/testkit': '~9.
|
|
30
|
+
'@geekmidas/audit': '~9.3.0',
|
|
31
|
+
'@geekmidas/auth': '~9.3.0',
|
|
32
|
+
'@geekmidas/cache': '~9.3.0',
|
|
33
|
+
'@geekmidas/client': '~9.3.0',
|
|
34
|
+
'@geekmidas/cloud': '~9.3.0',
|
|
35
|
+
'@geekmidas/constructs': '~9.3.0',
|
|
36
|
+
'@geekmidas/db': '~9.3.0',
|
|
37
|
+
'@geekmidas/emailkit': '~9.3.0',
|
|
38
|
+
'@geekmidas/envkit': '~9.3.0',
|
|
39
|
+
'@geekmidas/errors': '~9.3.0',
|
|
40
|
+
'@geekmidas/events': '~9.3.0',
|
|
41
|
+
'@geekmidas/logger': '~9.3.0',
|
|
42
|
+
'@geekmidas/rate-limit': '~9.3.0',
|
|
43
|
+
'@geekmidas/schema': '~9.3.0',
|
|
44
|
+
'@geekmidas/services': '~9.3.0',
|
|
45
|
+
'@geekmidas/storage': '~9.3.0',
|
|
46
|
+
'@geekmidas/studio': '~9.3.0',
|
|
47
|
+
'@geekmidas/telescope': '~9.3.0',
|
|
48
|
+
'@geekmidas/testkit': '~9.3.0',
|
|
49
49
|
'@geekmidas/cli': CLI_VERSION,
|
|
50
50
|
} as const;
|
|
51
51
|
|