Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add headers to webhook data #108

Merged
merged 4 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kontent-ai/data-ops",
"version": "2.2.2",
"version": "2.3.0",
"description": "",
"type": "module",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/backupRestore/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as fs from "fs";
import packageFile from "../../../package.json" with { type: "json" };
import { logInfo, LogOptions } from "../../log.js";
import { createClient } from "../../utils/client.js";
import { serializeDateForFileName } from "../../utils/files.js";
import { DateLevel, serializeDateForFileName } from "../../utils/files.js";
import { serially } from "../../utils/requests.js";
import { assetFoldersEntity } from "./backupRestoreEntities/entities/assetFolders.js";
import { assetsEntity } from "./backupRestoreEntities/entities/assets.js";
Expand Down Expand Up @@ -90,7 +90,7 @@ export const backupEnvironmentInternal = async (

const now = new Date();
const fileName = params.fileName
?? `${serializeDateForFileName(now)}-backup-${params.environmentId}.zip`;
?? `${serializeDateForFileName(now, DateLevel.Minute)}-backup-${params.environmentId}.zip`;

const outputStream = fs.createWriteStream(fileName);
const archive = archiver("zip");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const webhooksEntity = {
secret: webhook.secret,
url: webhook.url,
enabled: webhook.enabled,
headers: webhook.headers,
delivery_triggers: transformReferences(
webhook.delivery_triggers,
simplifyContext(context, [
Expand Down
6 changes: 1 addition & 5 deletions src/modules/migrations/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { existsSync } from "fs";
import * as path from "path";

import { logInfo, LogOptions } from "../../log.js";
import { padWithLeadingZeros } from "../../utils/number.js";
import { MigrationModuleType } from "./models/migration.js";
import { handleErr } from "./utils/errUtils.js";
import { createFolder, saveFile } from "./utils/fileUtils.js";
Expand Down Expand Up @@ -56,11 +57,6 @@ export const addMigration = async (params: AddMigrationParams) => {
);
};

const padWithLeadingZeros = (order: number, numberOfZeros: number | undefined) =>
numberOfZeros
? order.toString().padStart(numberOfZeros, "0")
: order.toString();

type TimestampOrOrderParams =
| Readonly<{ timestamp: true }>
| Readonly<{ timestamp?: false } & OrderParams>;
Expand Down
11 changes: 2 additions & 9 deletions src/modules/migrations/utils/migrationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import chalk from "chalk";
import { match, P } from "ts-pattern";

import { logError, logInfo, LogOptions } from "../../../log.js";
import { DateLevel, serializeDateForFileName } from "../../../utils/files.js";
import { seriallyReduce } from "../../../utils/requests.js";
import { isMigrationModule, Migration, MigrationModuleType, MigrationOrder } from "../models/migration.js";
import { MigrationOperation, MigrationStatus } from "../models/status.js";
Expand All @@ -15,16 +16,8 @@ import { WithErr } from "./errUtils.js";
import { orderComparator } from "./orderUtils.js";
import { createMigrationStatus } from "./statusUtils.js";

export const formatDateForFileName = (date: Date) =>
`${date.getUTCFullYear()}-`
+ `${("0" + (date.getUTCMonth() + 1)).slice(-2)}-`
+ `${("0" + date.getUTCDate()).slice(-2)}-`
+ `${("0" + date.getUTCHours()).slice(-2)}-`
+ `${("0" + date.getUTCMinutes()).slice(-2)}-`
+ `${("0" + date.getUTCSeconds()).slice(-2)}-`;

export const getMigrationName = (name: string, type: MigrationModuleType, prefix: Date | string | undefined) =>
`${prefix instanceof Date ? formatDateForFileName(prefix) : prefix ?? ""}${name}.${type}`;
`${prefix instanceof Date ? `${serializeDateForFileName(prefix, DateLevel.Second)}-` : prefix ?? ""}${name}.${type}`;

export const generateTypescriptMigration = (order: Date | number | undefined): string =>
`import { MigrationModule } from "@kontent-ai/data-ops";
Expand Down
4 changes: 2 additions & 2 deletions src/modules/sync/generateSyncModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as path from "path";

import packageJson from "../../../package.json" with { type: "json" };
import { logInfo, LogOptions } from "../../log.js";
import { serializeDateForFileName } from "../../utils/files.js";
import { DateLevel, serializeDateForFileName } from "../../utils/files.js";
import { notNullOrUndefined } from "../../utils/typeguards.js";
import { syncEntityChoices, SyncEntityName } from "./constants/entities.js";
import {
Expand Down Expand Up @@ -168,7 +168,7 @@ export const saveSyncModel = async (params: SaveModelParams) => {
generatedFromEnvironmentId: params.environmentId,
},
};
const folderName = params.folderName ?? `${serializeDateForFileName(now)}-${params.environmentId}`;
const folderName = params.folderName ?? `${serializeDateForFileName(now, DateLevel.Minute)}-${params.environmentId}`;

logInfo(params, "standard", `Saving the model into a folder "${chalk.yellow(folderName)}".`);

Expand Down
31 changes: 27 additions & 4 deletions src/utils/files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
export const serializeDateForFileName = (date: Date) =>
`${date.getUTCFullYear()}-${
date.getUTCMonth() + 1
}-${date.getUTCDate()}-${date.getUTCHours()}-${date.getUTCMinutes()}`;
import { padWithLeadingZeros } from "./number.js";

export enum DateLevel {
Year = 0,
Month = 1,
Day = 2,
Hour = 3,
Minute = 4,
Second = 5,
}

export const serializeDateForFileName = (date: Date, level: DateLevel) =>
createDateParts(date)
.slice(0, level + 1)
.join("-");

const createDateParts = (date: Date) =>
Object.values(
{
[DateLevel.Year]: date.getUTCFullYear().toString(),
[DateLevel.Month]: padWithLeadingZeros(date.getUTCMonth() + 1, 2),
IvanKiral marked this conversation as resolved.
Show resolved Hide resolved
[DateLevel.Day]: padWithLeadingZeros(date.getUTCDate(), 2),
[DateLevel.Hour]: padWithLeadingZeros(date.getUTCHours(), 2),
[DateLevel.Minute]: padWithLeadingZeros(date.getUTCMinutes(), 2),
[DateLevel.Second]: padWithLeadingZeros(date.getUTCSeconds(), 2),
} as const satisfies Record<DateLevel, string>,
);
11 changes: 11 additions & 0 deletions src/utils/number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Pads a given positive number with leading zeros up to the specified length.
*
* @param {number} num - The positive number to pad. Must be greater than or equal to 0.
* @param {number | undefined} numberOfZeros - The total length of the resulting string, including leading zeros. If undefined, the number is returned as is.
* @returns {string} The number as a string, padded with leading zeros if specified.
*/
export const padWithLeadingZeros = (num: number, numberOfZeros: number | undefined): string =>
numberOfZeros
? num.toString().padStart(numberOfZeros, "0")
: num.toString();
20 changes: 20 additions & 0 deletions tests/unit/utils/files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, it } from "vitest";

import { DateLevel, serializeDateForFileName } from "../../../src/utils/files.js";

describe("serializeDateForFileName", () => {
it.each([
{ result: "2025", level: DateLevel[DateLevel.Year] },
{ result: "2025-02", level: DateLevel[DateLevel.Month] },
{ result: "2025-02-07", level: DateLevel[DateLevel.Day] },
{ result: "2025-02-07-15", level: DateLevel[DateLevel.Hour] },
{ result: "2025-02-07-15-08", level: DateLevel[DateLevel.Minute] },
{ result: "2025-02-07-15-08-40", level: DateLevel[DateLevel.Second] },
])(
`correctly serializes date 2025-02-07T15:08:40Z to $result with DateLevel $level`,
({ result, level }) => {
const date = new Date("2025-02-07T15:08:40Z");
expect(serializeDateForFileName(date, DateLevel[level as keyof typeof DateLevel])).toEqual(result);
},
);
});
21 changes: 21 additions & 0 deletions tests/unit/utils/number.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, it } from "vitest";

import { padWithLeadingZeros } from "../../../src/utils/number.js";

describe("padWithLeadingZeros", () => {
it.each(
[
{ input: [5, 3], expected: "005" },
{ input: [42, 5], expected: "00042" },
{ input: [0, 4], expected: "0000" },
{ input: [123, 2], expected: "123" },
{ input: [4567, 4], expected: "4567" },
{ input: [5, 0], expected: "5" },
{ input: [8, undefined], expected: "8" },
{ input: [123, undefined], expected: "123" },
] as const,
)("should return $expected for input $input", ({ input, expected }) => {
const [num, leadingZeros] = input;
expect(padWithLeadingZeros(num, leadingZeros)).toBe(expected);
});
});
Loading