Skip to content

Commit

Permalink
feat: services api (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
productdevbook authored Nov 26, 2023
1 parent cc2fd98 commit 432707c
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ export function easypanel(config: ClientConfig) {
const resolvedClient = client(config)

const projects = projectsManager(resolvedClient)
const services = projectsManager(resolvedClient)

return {
projects,
services,
}
}
289 changes: 289 additions & 0 deletions src/managers/ServicesManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
import type { ClientResponse, NoResponse, StringResponse } from '../typesNew'
import type {
CreateService,
DeployParams,
MountParams,
SelectService,
ServiceRes,
ServiceType,
UpdateBasicAuth,
UpdateBuild,
UpdateEnv,
UpdateGit,
UpdateGithub,
UpdateImage,
UpdatePorts,
UpdateRedirects,
UpdateResources,
} from '../typesNew/services'
import { Routes } from '../utils/Routes'

export function ServicesManager({ get, post }: ClientResponse) {
/**
* Creates a new service.
*/
async function create(
serviceType: ServiceType,
body: CreateService,
) {
body.domains = [
{
host: '$(EASYPANEL_DOMAIN)',
},
]
const Route = Routes.Services(serviceType).Create
const res = await post<ServiceRes>(Route, { json: body })
return res
}

/**
* Retrieves information about a service.
*/
async function inspect(
serviceType: ServiceType,
body: SelectService,
) {
const Route = Routes.Services(serviceType).Inspect
const res = await get<ServiceRes>(Route, { json: body })
return res
}

/**
* Destroys a service.
*/
async function destroy(
serviceType: ServiceType,
body: SelectService,
) {
const Route = Routes.Services(serviceType).Destroy
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Deploys a service.
*/
async function deploy(
serviceType: ServiceType,
body: SelectService,
) {
const Route = Routes.Services(serviceType).Deploy
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Disables a service.
*/
async function disable(
serviceType: ServiceType,
body: SelectService,
) {
const Route = Routes.Services(serviceType).Disable
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Enables a service.
*/
async function enable(
serviceType: ServiceType,
body: SelectService,
) {
const Route = Routes.Services(serviceType).Enable
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Refreshes the deploy token for a service.
*/
async function refreshDeployToken(
serviceType: ServiceType,
body: SelectService,
) {
const Route = Routes.Services(serviceType).RefreshDeployToken
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Updates the source from GitHub for a service.
*/
async function updateSourceGithub(
serviceType: ServiceType,
body: UpdateGithub,
) {
const Route = Routes.Services(serviceType).UpdateSourceGithub
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Updates the source from Git for a service.
*/
async function updateSourceGit(
serviceType: ServiceType,
body: UpdateGit,
) {
const Route = Routes.Services(serviceType).UpdateSourceGit
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Updates the source from an image for a service.
*/
async function updateSourceImage(
serviceType: ServiceType,
body: UpdateImage,
) {
const Route = Routes.Services(serviceType).UpdateSourceImage
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Updates the build configuration for a service.
*/
async function updateBuild(
serviceType: ServiceType,
body: UpdateBuild,
) {
const Route = Routes.Services(serviceType).UpdateBuild
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Updates the environment variables for a service.
*/
async function updateEnv(
serviceType: ServiceType,
body: UpdateEnv,
) {
const Route = Routes.Services(serviceType).UpdateEnv
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Updates the domain configuration for a service.
*/
async function updateDomains(
serviceType: ServiceType,
body: CreateService,
) {
const Route = Routes.Services(serviceType).UpdateDomains
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Updates the redirects configuration for a service.
*/
async function updateRedirects(
serviceType: ServiceType,
body: UpdateRedirects,
) {
const Route = Routes.Services(serviceType).UpdateRedirects
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Updates the basic authentication configuration for a service.
*/
async function updateBasicAuth(
serviceType: ServiceType,
body: UpdateBasicAuth,
) {
const Route = Routes.Services(serviceType).UpdateBasicAuth
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Updates the mounts configuration for a service.
*/
async function updateMounts(
serviceType: ServiceType,
body: MountParams,
) {
const Route = Routes.Services(serviceType).UpdateMounts
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Updates the ports configuration for a service.
*/
async function updatePorts(
serviceType: ServiceType,
body: UpdatePorts,
) {
const Route = Routes.Services(serviceType).UpdatePorts
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Updates the resources configuration for a service.
*/
async function updateResources(
serviceType: ServiceType,
body: UpdateResources,
) {
const Route = Routes.Services(serviceType).UpdateResources
const res = await post<NoResponse>(Route, { json: body })
return res
}

/**
* Updates the deployment configuration for a service.
*/
async function updateDeploy(
serviceType: ServiceType,
body: DeployParams,
) {
const Route = Routes.Services(serviceType).UpdateDeploy
const res = await post<NoResponse>(Route, { json: body })
return res
}

async function getServiceLogs(body: SelectService) {
const service = `${body.projectName}_${body.serviceName}`
const res = await get<StringResponse>(Routes.Services('').GetServiceLogs, {
json: {
service,
lines: 50,
},
})

return res
}

return {
create,
inspect,
destroy,
deploy,
disable,
enable,
refreshDeployToken,
updateSourceGithub,
updateSourceGit,
updateSourceImage,
updateBuild,
updateEnv,
updateDomains,
updateRedirects,
updateBasicAuth,
updateMounts,
updatePorts,
updateResources,
updateDeploy,
getServiceLogs,
}
}
1 change: 1 addition & 0 deletions src/managers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './ProjectsManager'
export * from './ServicesManager'
4 changes: 1 addition & 3 deletions src/typesNew/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ export interface ProjectQueryConf {
projectName: string
}

export type CanCreate = RestResponse<{
canCreate: boolean
}>
export type CanCreate = RestResponse<boolean>

export type Create = RestResponse<ProjectInfo>

Expand Down
4 changes: 1 addition & 3 deletions src/typesNew/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,4 @@ export interface UpdateResources extends SelectService {
}
}

export type ListServices = RestResponse<{
services: Service[]
}>
export type ServiceRes = RestResponse<Service>

0 comments on commit 432707c

Please sign in to comment.