Skip to content

Commit

Permalink
feat: getUser, getLicensePayload, activateLicense (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
productdevbook authored Nov 26, 2023
1 parent e6e435e commit 87b6ec7
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 142 deletions.
37 changes: 28 additions & 9 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ofetch } from 'ofetch'
import type { ClientConfig, ClientResponse } from './typesNew'
import { projectsManager, servicesManager } from './managers'
import { monitorManager } from './managers/MonitorManager'
import { settingsManager } from './managers/SettingsManager'
import type { ClientConfig, ClientResponse, NoResponse, UserRes } from './types'
import { monitorManager, projectsManager, servicesManager, settingsManager } from './managers'
import { Routes } from './utils/Routes'

let token = ''

Expand Down Expand Up @@ -47,17 +46,37 @@ function client(config: ClientConfig): ClientResponse {
}

export function easypanel(config: ClientConfig) {
const resolvedClient = client(config)
const _client = client(config)

const projects = projectsManager(resolvedClient)
const services = servicesManager(resolvedClient)
const monitor = monitorManager(resolvedClient)
const settings = settingsManager(resolvedClient)
const projects = projectsManager(_client)
const services = servicesManager(_client)
const monitor = monitorManager(_client)
const settings = settingsManager(_client)

async function getUser(): Promise<UserRes> {
const res = await _client.get<UserRes>(Routes.Auth.GetUser, { json: null })
return res
}

async function getLicensePayload(type: 'lemon' | 'portal'): Promise<NoResponse> {
const res = await _client.get<NoResponse>(Routes.License(type).Get, { json: null })
return res
}

async function activateLicense(type: 'lemon' | 'portal'): Promise<NoResponse> {
const res = await _client.post<NoResponse>(Routes.License(type).Activate, {
json: null,
})
return res
}

return {
projects,
services,
monitor,
settings,
getUser,
getLicensePayload,
activateLicense,
}
}
4 changes: 2 additions & 2 deletions src/managers/MonitorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type {
ContainerStats,
DockerTaskStats,
SystemStats,
} from '../typesNew/monitor'
import type { ClientResponse } from '../typesNew'
} from '../types/monitor'
import type { ClientResponse } from '../types'
import { Routes } from '../utils/Routes'

export function monitorManager({ get }: ClientResponse) {
Expand Down
2 changes: 1 addition & 1 deletion src/managers/ProjectsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
Project,
ProjectName,
ProjectQueryConf,
} from '../typesNew'
} from '../types'

export function projectsManager({ get, post }: ClientResponse) {
async function canCreate() {
Expand Down
4 changes: 2 additions & 2 deletions src/managers/ServicesManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ClientResponse, NoResponse, StringResponse } from '../typesNew'
import type { ClientResponse, NoResponse, StringResponse } from '../types'
import type {
CreateService,
DeployParams,
Expand All @@ -15,7 +15,7 @@ import type {
UpdatePorts,
UpdateRedirects,
UpdateResources,
} from '../typesNew/services'
} from '../types/services'
import { Routes } from '../utils/Routes'

export function servicesManager({ get, post }: ClientResponse) {
Expand Down
2 changes: 1 addition & 1 deletion src/managers/SettingsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
PruneDockerDailyParams,
StringResponse,
TreafikConfParams,
} from '../typesNew'
} from '../types'
import { Routes } from '../utils/Routes'

export function settingsManager({ get, post }: ClientResponse) {
Expand Down
2 changes: 2 additions & 0 deletions src/managers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './ProjectsManager'
export * from './ServicesManager'
export * from './SettingsManager'
export * from './MonitorManager'
58 changes: 0 additions & 58 deletions src/types/general.ts

This file was deleted.

62 changes: 51 additions & 11 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,58 @@
import type { $Fetch } from 'ofetch'
export interface ClientResponse {
get: <T>(route: string, body: any) => Promise<T>
post: <T>(route: string, body: any) => Promise<T>
token: string
}

export type * from './general'
export interface RestConfig {
baseURL: string
token: string
}

export interface ConstructorInput {
url: string
auth: {
email: string
password: string
export interface RestResponse<T> {
result: {
data: {
json: T
}
}
token?: string
}

export interface Options {
export interface RestError {
ok: boolean
errorMessage: string
status?: number
error?: object
}

export type * from './monitor'
export type * from './projects'
export type * from './services'
export type * from './settings'

export interface ClientConfig {
endpoint: string
token: string
nitro: $Fetch
auth: ConstructorInput['auth']
/**
* @deprecated Use `token` instead
*/
credentials?: {
email: string
password: string
}
}

export type NoResponse = RestResponse<null>

export type StringResponse = RestResponse<string>

export type BooleanResponse = RestResponse<boolean>

export interface LoginRes extends RestResponse<{ token: string }> {}

export interface UserRes extends RestResponse<{
id: string
createdAt: string
email: string
admin: boolean
password: null
}> {}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
58 changes: 0 additions & 58 deletions src/typesNew/index.ts

This file was deleted.

0 comments on commit 87b6ec7

Please sign in to comment.