
Research
/Security News
Weaponizing Discord for Command and Control Across npm, PyPI, and RubyGems.org
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.
@clipboard-health/util-typescript
Advanced tools
TypeScript utilities.
npm install @clipboard-health/util-typescript
See ./src/lib
for each utility.
// ./examples/serviceError.ts
import { deepEqual, equal } from "node:assert/strict";
import { ERROR_CODES, ServiceError } from "@clipboard-health/util-typescript";
{
const error = new ServiceError("boom");
equal(error.toString(), `ServiceError[${error.id}]: [internal]: boom`);
}
{
const error = new Error("boom");
const serviceError = ServiceError.fromError(error);
equal(
serviceError.toString(),
`ServiceError[${serviceError.id}]: [internal]: boom; [cause]: Error: boom`,
);
}
{
const errorWithCause = new ServiceError({
issues: [{ message: "boom" }],
cause: new Error("Original error"),
});
equal(
errorWithCause.toString(),
`ServiceError[${errorWithCause.id}]: [internal]: boom; [cause]: Error: Original error`,
);
}
{
const multipleIssues = new ServiceError({
issues: [
{
code: ERROR_CODES.badRequest,
message: "Invalid email format",
path: ["data", "attributes", "email"],
},
{
code: ERROR_CODES.unprocessableEntity,
message: "Phone number too short",
path: ["data", "attributes", "phoneNumber"],
},
],
cause: new Error("Original error"),
});
equal(
multipleIssues.toString(),
`ServiceError[${multipleIssues.id}]: [badRequest]: Invalid email format; [unprocessableEntity]: Phone number too short; [cause]: Error: Original error`,
);
deepEqual(multipleIssues.toJsonApi(), {
errors: [
{
id: multipleIssues.id,
status: "400",
code: "badRequest",
title: "Invalid or malformed request",
detail: "Invalid email format",
source: {
pointer: "/data/attributes/email",
},
},
{
id: multipleIssues.id,
status: "422",
code: "unprocessableEntity",
title: "Request failed validation",
detail: "Phone number too short",
source: {
pointer: "/data/attributes/phoneNumber",
},
},
],
});
}
// ./examples/serviceResult.ts
import { ok } from "node:assert/strict";
import {
either as E,
ERROR_CODES,
failure,
type ServiceResult,
success,
} from "@clipboard-health/util-typescript";
function validateUser(params: { email: string; phone: string }): ServiceResult<{ id: string }> {
const { email, phone } = params;
const code = ERROR_CODES.unprocessableEntity;
if (!email.includes("@")) {
return failure({ issues: [{ code, message: "Invalid email format" }] });
}
if (phone.length !== 12) {
return failure({ issues: [{ code, message: "Invalid phone number" }] });
}
return success({ id: "user-123" });
}
ok(E.isLeft(validateUser({ email: "invalidEmail", phone: "invalidPhoneNumber" })));
ok(E.isRight(validateUser({ email: "user@example.com", phone: "555-555-5555" })));
pipe
// ./examples/pipe.ts
import { equal } from "node:assert/strict";
import { pipe } from "@clipboard-health/util-typescript";
const result = pipe(
" hello world ",
(s) => s.trim(),
(s) => s.split(" "),
(array) => array.map((word) => word.charAt(0).toUpperCase() + word.slice(1)),
(array) => array.join(" "),
);
equal(result, "Hello World");
option
// ./examples/option.ts
import { equal } from "node:assert/strict";
import { option as O, pipe } from "@clipboard-health/util-typescript";
function double(n: number) {
return n * 2;
}
function inverse(n: number): O.Option<number> {
return n === 0 ? O.none : O.some(1 / n);
}
const result = pipe(
O.some(5),
O.map(double),
O.flatMap(inverse),
O.match(
() => "No result",
(n) => `Result is ${n}`,
),
);
equal(result, "Result is 0.1");
either
// ./examples/either.ts
import { equal } from "node:assert/strict";
import { either as E, pipe } from "@clipboard-health/util-typescript";
function double(n: number): number {
return n * 2;
}
function inverse(n: number): E.Either<string, number> {
return n === 0 ? E.left("Division by zero") : E.right(1 / n);
}
const result = pipe(
E.right(5),
E.map(double),
E.flatMap(inverse),
E.match(
(error) => `Error: ${error}`,
(result) => `Result is ${result}`,
),
);
equal(result, "Result is 0.1");
See package.json
scripts
for a list of commands.
FAQs
TypeScript utilities.
We found that @clipboard-health/util-typescript demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.
Security News
Socket now integrates with Bun 1.3’s Security Scanner API to block risky packages at install time and enforce your organization’s policies in local dev and CI.
Research
The Socket Threat Research Team is tracking weekly intrusions into the npm registry that follow a repeatable adversarial playbook used by North Korean state-sponsored actors.