powr-sdk-api 4.16.0 → 4.16.1

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.
@@ -153,7 +153,7 @@ router.post("/", async (req, res) => {
153
153
  projectId: String(req.projectId),
154
154
  kind: "http",
155
155
  ...fields,
156
- nextRun: fields.enabled ? now : null,
156
+ nextRun: fields.enabled ? nextRunFromCron(fields.cron, now) : null,
157
157
  lastRunAt: null,
158
158
  lastOk: null,
159
159
  lastStatus: null,
@@ -196,12 +196,33 @@ router.patch("/:id", async (req, res) => {
196
196
  try {
197
197
  const db = await getDb();
198
198
  if (!(await requireActor(db, req, res))) return;
199
+ const existing = await db.collection(COLLECTION).findOne({
200
+ _id: oid,
201
+ projectId: String(req.projectId)
202
+ });
203
+ if (!existing) {
204
+ return res.status(404).json({
205
+ success: false,
206
+ message: "Schedule not found."
207
+ });
208
+ }
199
209
  const now = new Date();
200
210
  const update = {
201
211
  ...patch,
202
212
  updatedAt: now
203
213
  };
204
- if (patch.enabled === true) update.nextRun = now;
214
+ const turningOn = patch.enabled === true;
215
+ const cronChangedWhileOn = patch.cron && existing.enabled === true && patch.enabled !== false;
216
+ if (turningOn || cronChangedWhileOn) {
217
+ try {
218
+ update.nextRun = nextRunFromCron(patch.cron || existing.cron, now);
219
+ } catch (error) {
220
+ return res.status(error.statusCode || 400).json({
221
+ success: false,
222
+ message: error.message
223
+ });
224
+ }
225
+ }
205
226
  const result = await db.collection(COLLECTION).findOneAndUpdate({
206
227
  _id: oid,
207
228
  projectId: String(req.projectId)
@@ -106,18 +106,39 @@ function parseMethod(value) {
106
106
  if (method !== "GET" && method !== "POST") reject("Method must be GET or POST.");
107
107
  return method;
108
108
  }
109
+ function parseKeyEnv(value) {
110
+ if (value == null || value === "") return null;
111
+ const name = String(value).trim();
112
+ if (!/^[A-Z][A-Z0-9_]{0,63}$/.test(name)) {
113
+ reject("Service key env must be an uppercase env name.");
114
+ }
115
+ return name;
116
+ }
117
+ function configuredServiceKey(row, env) {
118
+ const name = row && row.keyEnv;
119
+ if (!name) return "";
120
+ if (!/^[A-Z][A-Z0-9_]{0,63}$/.test(String(name))) {
121
+ reject("Service key is not configured.");
122
+ }
123
+ const value = env && env[name];
124
+ if (!value) reject("Service key is not configured.");
125
+ return String(value);
126
+ }
109
127
  function parseScheduleCreate(body) {
110
128
  const source = body || {};
111
129
  const name = parseName(source.name);
112
130
  const cron = String(source.cron || "").trim();
113
131
  nextRunFromCron(cron, new Date());
114
- return {
132
+ const parsed = {
115
133
  name,
116
134
  cron,
117
135
  method: parseMethod(source.method),
118
136
  url: assertPublicHttpsUrl(source.url),
119
137
  enabled: source.enabled === true
120
138
  };
139
+ const keyEnv = parseKeyEnv(source.keyEnv);
140
+ if (keyEnv) parsed.keyEnv = keyEnv;
141
+ return parsed;
121
142
  }
122
143
  function parseSchedulePatch(body) {
123
144
  const source = body || {};
@@ -134,6 +155,11 @@ function parseSchedulePatch(body) {
134
155
  if (typeof source.enabled !== "boolean") reject("Enabled must be true or false.");
135
156
  patch.enabled = source.enabled;
136
157
  }
158
+ if (source.keyEnv !== undefined) {
159
+ const keyEnv = parseKeyEnv(source.keyEnv);
160
+ if (!keyEnv) reject("Service key env must be an uppercase env name.");
161
+ patch.keyEnv = keyEnv;
162
+ }
137
163
  if (!Object.keys(patch).length) reject("Nothing to update.");
138
164
  return patch;
139
165
  }
@@ -219,7 +245,7 @@ function updatedDoc(raw) {
219
245
  if (raw._id) return raw;
220
246
  return null;
221
247
  }
222
- async function fireTarget(row, fetchImpl = global.fetch) {
248
+ async function fireTarget(row, fetchImpl = global.fetch, env = process.env) {
223
249
  let url;
224
250
  try {
225
251
  url = assertPublicHttpsUrl(row && row.url);
@@ -240,6 +266,21 @@ async function fireTarget(row, fetchImpl = global.fetch) {
240
266
  error: error.message
241
267
  };
242
268
  }
269
+ let serviceKey = "";
270
+ if (row && row.keyEnv) {
271
+ try {
272
+ serviceKey = configuredServiceKey(row, env);
273
+ } catch (error) {
274
+ return {
275
+ ok: false,
276
+ status: null,
277
+ error: "Service key is not configured."
278
+ };
279
+ }
280
+ }
281
+ const headers = {};
282
+ if (method === "POST") headers["Content-Type"] = "application/json";
283
+ if (serviceKey) headers["X-Service-Key"] = serviceKey;
243
284
  const controller = new AbortController();
244
285
  const timer = setTimeout(() => controller.abort(), FIRE_MS);
245
286
  try {
@@ -248,9 +289,7 @@ async function fireTarget(row, fetchImpl = global.fetch) {
248
289
  redirect: "manual",
249
290
  cache: "no-store",
250
291
  signal: controller.signal,
251
- headers: method === "POST" ? {
252
- "Content-Type": "application/json"
253
- } : undefined,
292
+ headers: Object.keys(headers).length ? headers : undefined,
254
293
  body: method === "POST" ? "{}" : undefined
255
294
  });
256
295
  const status = Number(response && response.status);
@@ -24,6 +24,14 @@ describe("nextRunFromCron", () => {
24
24
  it("rejects a nickname", () => {
25
25
  expect(() => nextRunFromCron("@hourly")).toThrow("5-field cron");
26
26
  });
27
+ it("lands on 23:30 India time, not the next minute", () => {
28
+ const morning = new Date("2026-10-09T04:00:00.000Z");
29
+ const next = nextRunFromCron("30 23 * * *", morning);
30
+ expect(next.toISOString()).toBe("2026-10-09T18:00:00.000Z");
31
+ const afterClose = new Date("2026-10-09T18:00:01.000Z");
32
+ const tomorrow = nextRunFromCron("30 23 * * *", afterClose);
33
+ expect(tomorrow.toISOString()).toBe("2026-10-10T18:00:00.000Z");
34
+ });
27
35
  });
28
36
  describe("isDue", () => {
29
37
  const now = new Date("2026-10-09T10:01:00.000Z");
@@ -88,6 +96,24 @@ describe("parseScheduleCreate", () => {
88
96
  enabled: false
89
97
  });
90
98
  });
99
+ it("keeps an uppercase service-key env name", () => {
100
+ expect(parseScheduleCreate({
101
+ name: "Day close",
102
+ cron: "30 23 * * *",
103
+ method: "POST",
104
+ url: "https://example.com/ops/day-close",
105
+ keyEnv: "URBANSTALL_SERVICE_KEY"
106
+ }).keyEnv).toBe("URBANSTALL_SERVICE_KEY");
107
+ });
108
+ it("rejects a service-key value pasted as the env name", () => {
109
+ expect(() => parseScheduleCreate({
110
+ name: "Day close",
111
+ cron: "30 23 * * *",
112
+ method: "POST",
113
+ url: "https://example.com/ops/day-close",
114
+ keyEnv: "not-an-env-name"
115
+ })).toThrow("uppercase env name");
116
+ });
91
117
  });
92
118
  describe("parseSchedulePatch", () => {
93
119
  it("accepts on/off", () => {
@@ -202,6 +228,39 @@ describe("fireTarget", () => {
202
228
  expect(ok.ok).toBe(true);
203
229
  expect(ok.error).toBe(null);
204
230
  });
231
+ it("sends the product service key on POST and does not call when it is missing", async () => {
232
+ const fetchImpl = jest.fn(async () => ({
233
+ status: 200
234
+ }));
235
+ const ok = await fireTarget({
236
+ url: "https://example.com/ops/day-close",
237
+ method: "POST",
238
+ keyEnv: "URBANSTALL_SERVICE_KEY"
239
+ }, fetchImpl, {
240
+ URBANSTALL_SERVICE_KEY: "test-key"
241
+ });
242
+ expect(ok.ok).toBe(true);
243
+ expect(fetchImpl).toHaveBeenCalledWith("https://example.com/ops/day-close", expect.objectContaining({
244
+ method: "POST",
245
+ body: "{}",
246
+ headers: {
247
+ "Content-Type": "application/json",
248
+ "X-Service-Key": "test-key"
249
+ }
250
+ }));
251
+ const skipped = jest.fn();
252
+ const missing = await fireTarget({
253
+ url: "https://example.com/ops/day-close",
254
+ method: "POST",
255
+ keyEnv: "URBANSTALL_SERVICE_KEY"
256
+ }, skipped, {});
257
+ expect(missing).toEqual({
258
+ ok: false,
259
+ status: null,
260
+ error: "Service key is not configured."
261
+ });
262
+ expect(skipped).not.toHaveBeenCalled();
263
+ });
205
264
  });
206
265
  describe("runDue", () => {
207
266
  it("fires a due row once and stores the fail", async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powr-sdk-api",
3
- "version": "4.16.0",
3
+ "version": "4.16.1",
4
4
  "description": "Shared API core library for PowrStack projects. Zero dependencies - works with Express, Next.js API routes, and other frameworks. All features are optional and install only what you need.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",