ecs-pf
Version:
CLI for port-forwarding to RDS via AWS ECS
77 lines (76 loc) • 3.85 kB
JavaScript
import { isFunction } from "remeda";
import { array, brand, integer, literal, maxValue, minLength, minValue, number, object, optional, pipe, regex, string, transform, union, unknown, } from "valibot";
import { AWS_REGION_NAME, DB_ENDPOINT_FORMAT, DIGITS_ONLY } from "../regex.js";
export const PortSchema = pipe(union([
pipe(string(), regex(DIGITS_ONLY, "Port must be a number"), transform(Number)),
number(),
]), integer("Port must be an integer"), minValue(1, "Port must be greater than 0"), maxValue(65535, "Port must be less than 65536"), brand("Port"));
export const RegionNameSchema = pipe(string(), minLength(1, "Region name cannot be empty"), regex(AWS_REGION_NAME, "Invalid region name format"), brand("RegionName"));
export const ClusterNameSchema = pipe(string(), minLength(1, "Cluster name cannot be empty"), transform((cluster) => {
if (cluster.trim() !== cluster) {
throw new Error("Cluster name cannot have leading or trailing whitespace");
}
return cluster;
}), brand("ClusterName"));
export const ClusterArnSchema = pipe(string(), minLength(1, "Cluster ARN cannot be empty"), brand("ClusterArn"));
export const TaskArnSchema = pipe(string(), minLength(1, "Task ARN cannot be empty"), brand("TaskArn"));
export const TaskIdSchema = pipe(string(), minLength(1, "Task ID cannot be empty"), brand("TaskId"));
export const RuntimeIdSchema = pipe(string(), minLength(1, "Runtime ID cannot be empty"), brand("RuntimeId"));
export const ServiceNameSchema = pipe(string(), minLength(1, "Service name cannot be empty"), brand("ServiceName"));
export const ServiceArnSchema = pipe(string(), minLength(1, "Service ARN cannot be empty"), brand("ServiceArn"));
export const DBInstanceIdentifierSchema = pipe(string(), minLength(1, "DB Instance identifier cannot be empty"), brand("DBInstanceIdentifier"));
export const DBEndpointSchema = pipe(string(), minLength(1, "DB endpoint cannot be empty"), regex(DB_ENDPOINT_FORMAT, "Invalid DB endpoint format"), brand("DBEndpoint"));
export const ContainerNameSchema = pipe(string(), minLength(1, "Container name cannot be empty"), brand("ContainerName"));
export const DatabaseEngineSchema = pipe(string(), minLength(1, "Database engine cannot be empty"), brand("DatabaseEngine"));
export const CommandSchema = pipe(string(), minLength(1, "Command cannot be empty"));
export const OptionalCommandSchema = optional(CommandSchema);
export const NonEmptyStringSchema = pipe(string(), minLength(1, "String cannot be empty"));
export const VpcSecurityGroupsSchema = pipe(union([string(), array(string())]), transform((groups) => (Array.isArray(groups) ? groups : [groups])));
export const TaskStatusSchema = union([
literal("PROVISIONING"),
literal("PENDING"),
literal("ACTIVATING"),
literal("RUNNING"),
literal("DEACTIVATING"),
literal("STOPPING"),
literal("DEPROVISIONING"),
literal("STOPPED"),
]);
export const DBInstanceStatusSchema = union([
literal("available"),
literal("backing-up"),
literal("creating"),
literal("deleting"),
literal("maintenance"),
literal("modifying"),
literal("rebooting"),
literal("starting"),
literal("stopped"),
literal("stopping"),
]);
export const ECSClientSchema = pipe(object({
send: unknown(),
}), transform((input) => {
if (!isFunction(input.send)) {
throw new Error("Invalid ECS Client: send method is not a function");
}
return input;
}));
export function success(data) {
return { success: true, data };
}
export function failure(error) {
return { success: false, error };
}
export function isSuccess(result) {
return result.success;
}
export function isFailure(result) {
return !result.success;
}
export function unwrapBrandedString(value) {
return value;
}
export function unwrapBrandedNumber(value) {
return value;
}