Skip to content

Commit

Permalink
feat: add ignite zod schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
alii committed Feb 8, 2023
1 parent 63577bc commit 4fc901b
Show file tree
Hide file tree
Showing 6 changed files with 424 additions and 264 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@
"devDependencies": {
"@changesets/cli": "2.26.0",
"@types/glob": "8.0.1",
"@types/node": "18.11.18",
"@types/node": "18.13.0",
"dotenv": "16.0.3",
"glob": "8.1.0",
"prettier": "2.8.3",
"tsup": "6.5.0",
"tsx": "3.12.2",
"prettier": "2.8.4",
"tsup": "6.6.0",
"tsx": "3.12.3",
"typedoc": "0.23.24",
"typedoc-plugin-markdown": "3.14.0",
"typedoc-plugin-missing-exports": "1.0.0",
"typescript": "^4.9.4"
"typescript": "^4.9.5"
},
"dependencies": {
"@onehop/json-methods": "^1.2.0",
"cross-fetch": "^3.1.5",
"zod": "^3.20.2"
"zod": "^3.20.3"
}
}
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
export * from './hop.js';
export * from './permissions.js';
export * from './rest/index.js';
// Commonly used in the API, so nice to export in two place
export {type APIAuthentication} from './rest/index.js';
export {ChannelType} from './rest/types/channels.js';
export {
BuildMethod,
BuildState,
ContainerState,
DomainState,
RestartPolicy,
RolloutState,
RuntimeType,
BuildState,
DomainState,
BuildMethod,
VolumeFormat,
type BuildEnvironment,
} from './rest/types/ignite.js';
export * from './util/index.js';
23 changes: 18 additions & 5 deletions src/rest/types/ignite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,23 @@ export type DeploymentRollout = {

export type CreateDeploymentConfig = MakeOptional<DeploymentConfig, 'cmd'>;

/**
* The strategy for scaling multiple containers.
* @warning This property is not yet fully complete
*/
export enum ContainerStrategy {
/**
* Add containers yourself with the API or Console
*/
MANUAL = 'manual',

/**
* Have Hop automatically scale containers based on load
* @warning This is incomplete
*/
// AUTOSCALE = 'autoscale',
}

export interface DeploymentConfig {
/**
* The name of the deployment
Expand All @@ -530,12 +547,8 @@ export interface DeploymentConfig {

/**
* The strategy for scaling multiple containers.
*
* Manual = add containers yourself
*
* @warning This property is not yet fully complete
*/
container_strategy: 'manual';
container_strategy: ContainerStrategy;

/**
* The type of this deployment
Expand Down
136 changes: 136 additions & 0 deletions src/utils/zod/ignite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import {z} from 'zod';
import {
ContainerStrategy,
RestartPolicy,
RuntimeType,
VolumeFormat,
} from '../../rest/types/ignite.js';
import {byteUnits, parseSize, isValidByteString} from '../../util/size.js';

export const deploymentMetaDataSchema = z.object({
preset: z.string().optional(),
next_steps_dismissed: z.boolean().optional(),
container_port_mappings: z.record(z.string(), z.set(z.string())).optional(),
created_first_gateway: z.boolean().optional(),
ignored_boarding: z.boolean().optional(),
max_container_storage: z.string().optional(),
});

const UNIX_DIR_REGEX = /^\/([a-zA-Z0-9_\-]+\/)*([a-zA-Z0-9_\-]+)$/g;

const MIN_VOLUME_SIZE_BYTES = 1073741824; // 1gb
const MIN_RAM_SIZE_BYTES = 134217728;

export const volumeFormatSchema = z.nativeEnum(VolumeFormat);

export const volumeSchema = z
.object({
fs: volumeFormatSchema,
size: z
.string()
.transform(value => value.toUpperCase())
.refine(
isValidByteString,
`Must be a valid byte string, e.g. 1gb. Supported units are ${byteUnits.join(
', ',
)}`,
)
.refine(v => {
try {
const size = parseSize(v);

return size >= MIN_VOLUME_SIZE_BYTES;
} catch (err) {
return false;
}
}, `Volume size must be at least 1gb`)
.transform(v => v.toLowerCase()),
mountpath: z
.string()
.regex(
UNIX_DIR_REGEX,
"Must be a valid unix directory path, e.g. '/data'",
),
})
.required();

export const buildSettingsSchema = z.object({
root_directory: z
.string()
.regex(UNIX_DIR_REGEX, "Must be a valid unix directory path, e.g. '/data'")
.or(
z
.string()
.refine(v => v === '' || v === '/')
.transform(() => '/'),
),
});

export const deploymentRuntimeTypeSchema = z.nativeEnum(RuntimeType);

export const containerResourcesSchema = z.object({
vcpu: z.number().min(0.5).max(16).optional(),
cpu: z.number().min(0.5).max(16).optional(),
ram: z
.string()
.transform(value => value.toUpperCase())
.refine(isValidByteString)
.refine(v => {
const size = parseSize(v);
return size >= MIN_RAM_SIZE_BYTES;
})
.transform(v => v.toLowerCase()),
});

export const deploymentEnvSchema = z
.record(z.string().min(1).max(128), z.string())
.optional();

export const containerStrategySchema = z.nativeEnum(ContainerStrategy);

export const deploymentNameSchema = z
.string()
.regex(/^[a-zA-Z0-9-]*$/g, "Must be alphanumeric and can include '-'");

export const restartPolicySchema = z.nativeEnum(RestartPolicy);

export const deploymentConfigSchema = z.object({
version: z.string().default('2022-12-28'),
restart_policy: restartPolicySchema
.optional()
.default(RestartPolicy.ON_FAILURE),
type: deploymentRuntimeTypeSchema,
cmd: z.array(z.string()).optional(),
image: z.union([
z.object({
name: z.string(),
auth: z
.object({
username: z.string(),
password: z.string(),
})
.nullable()
.optional(),
}),
z.object({
name: z.string().optional(),
gh_repo: z.object({
repo_id: z.number(),
full_name: z.string(),
branch: z.string(),
}),
auth: z.null().optional(),
}),
]),
volume: volumeSchema.optional(),
env: deploymentEnvSchema,
entrypoint: z
.array(z.string())
.optional()
.transform(v => (!v?.length ? null : v))
.nullable(),
container_strategy: containerStrategySchema
.optional()
.default(ContainerStrategy.MANUAL),
resources: containerResourcesSchema,
});
1 change: 1 addition & 0 deletions src/utils/zod/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './ids.js';
export * from './presets.js';
export * from './ignite.js';
Loading

1 comment on commit 4fc901b

@vercel
Copy link

@vercel vercel bot commented on 4fc901b Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

hop-js – ./

hop-js-git-master-onehop.vercel.app
js.hop.io
hop-js.vercel.app
hop-js-onehop.vercel.app

Please sign in to comment.