Skip to content

Commit

Permalink
fix: refactor deno client to use another openapi generator windmill-l…
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenfiszel authored Oct 15, 2022
1 parent 6f629fc commit 350d31f
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 45 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/deno_on_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ env:
jobs:
build_deno_and_push_to_repo:
runs-on: ubuntu-latest
container: openapitools/openapi-generator-cli:v6.0.0-beta
steps:
- uses: actions/checkout@v3
- name: generate_deno
run: |
cd deno-client
rm .gitignore
./generate.sh
./build.sh
- name: Pushes to another repository
id: push_directory
uses: cpina/github-action-push-to-another-repository@devel
Expand Down
3 changes: 3 additions & 0 deletions deno-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# windmill-deno-client

Deno client for Windmill
10 changes: 10 additions & 0 deletions deno-client/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e

npx --yes openapi-typescript-codegen --input ../backend/openapi.yaml \
--output ./src --useOptions \
&& sed -i '213 i \\ request.referrerPolicy = \"no-referrer\"\n' src/core/request.ts
npx --yes denoify
rm -rf windmill-api
mv deno_dist windmill-api
rm -rf src/
6 changes: 0 additions & 6 deletions deno-client/generate.sh

This file was deleted.

59 changes: 29 additions & 30 deletions deno-client/mod.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ResourceApi, VariableApi, ServerConfiguration, JobApi } from './windmill-api/index.ts'
import { createConfiguration, type Configuration as Configuration } from './windmill-api/configuration.ts'
import { ResourceService, VariableService } from './windmill-api/index.ts'
import { OpenAPI } from './windmill-api/index.ts'

export {
AdminApi, AuditApi, FlowApi, GranularAclApi, GroupApi,
JobApi, ResourceApi, VariableApi, ScriptApi, ScheduleApi, SettingsApi,
UserApi, WorkspaceApi
AdminService, AuditService, FlowService, GranularAclService, GroupService,
JobService, ResourceService, VariableService, ScriptService, ScheduleService, SettingsService,
UserService, WorkspaceService
} from './windmill-api/index.ts'

export { pgSql, pgClient } from './pg.ts'
Expand All @@ -14,23 +14,22 @@ export type Email = string
export type Base64 = string
export type Resource<S extends string> = any

type Conf = Configuration & { workspace_id: string }

export const SHARED_FOLDER = '/shared'

export function setClient(token: string, baseUrl: string) {
OpenAPI.WITH_CREDENTIALS = true
OpenAPI.TOKEN = token
OpenAPI.BASE = baseUrl
}

setClient(Deno.env.get("WM_TOKEN") ?? 'no_token', Deno.env.get("BASE_INTERNAL_URL") ?? Deno.env.get("BASE_URL") ?? 'http://localhost:8000')

/**
* Create a client configuration from env variables
* @returns client configuration
*/
export function createConf(): Conf {
const token = Deno.env.get("WM_TOKEN") ?? 'no_token'
const base_url = Deno.env.get("BASE_INTERNAL_URL") ?? 'http://localhost:8000'
return {
...createConfiguration({
baseServer: new ServerConfiguration(`${base_url}/api`, {}),
authMethods: { bearerAuth: { tokenProvider: { getToken() { return token } } } },
}), workspace_id: Deno.env.get("WM_WORKSPACE") ?? 'no_workspace'
}
export function getWorkspace(): string {
return Deno.env.get("WM_WORKSPACE") ?? 'no_workspace'
}

/**
Expand All @@ -40,9 +39,9 @@ export function createConf(): Conf {
* @returns resource value
*/
export async function getResource(path: string, undefinedIfEmpty?: boolean): Promise<any> {
const conf = createConf()
const workspace = getWorkspace()
try {
const resource = await new ResourceApi(conf).getResource(conf.workspace_id, path)
const resource = await ResourceService.getResource({ workspace, path })
return await _transformLeaf(resource.value)
} catch (e) {
if (undefinedIfEmpty && e.code === 404) {
Expand Down Expand Up @@ -75,12 +74,11 @@ export function getInternalStatePath(suffix?: string): string {
* @param initializeToTypeIfNotExist if the resource does not exist, initialize it with this type
*/
export async function setResource(path: string, value: any, initializeToTypeIfNotExist?: string): Promise<void> {
const conf = createConf()
const resourceApi = new ResourceApi(conf)
if (await resourceApi.existsResource(conf.workspace_id, path)) {
await resourceApi.updateResource(conf.workspace_id, path, { value })
const workspace = getWorkspace()
if (await ResourceService.existsResource({ workspace, path })) {
await ResourceService.updateResource({ workspace, path, requestBody: { value } })
} else if (initializeToTypeIfNotExist) {
await resourceApi.createResource(conf.workspace_id, { path, value, resourceType: initializeToTypeIfNotExist })
await ResourceService.createResource({ workspace, requestBody: { path, value, resource_type: initializeToTypeIfNotExist } })
} else {
throw Error(`Resource at path ${path} does not exist and no type was provided to initialize it`)
}
Expand Down Expand Up @@ -109,8 +107,8 @@ export async function getInternalState(suffix?: string): Promise<any> {
* @returns variable value
*/
export async function getVariable(path: string): Promise<string | undefined> {
const conf = createConf()
const variable = await new VariableApi(conf).getVariable(conf.workspace_id, path)
const workspace = getWorkspace()
const variable = await VariableService.getVariable({ workspace, path })
return variable.value
}

Expand Down Expand Up @@ -140,24 +138,25 @@ export async function databaseUrlFromResource(path: string): Promise<string> {
}


export async function genNounceAndHmac(conf: Conf, jobId: string) {
export async function genNounceAndHmac(workspace: string, jobId: string) {
const nounce = Math.floor(Math.random() * 4294967295);
const sig = await fetch(Deno.env.get("WM_BASE_URL") +
`/api/w/${conf.workspace_id}/jobs/job_signature/${jobId}/${nounce}?token=${Deno.env.get("WM_TOKEN")}`)
`/api/w/${workspace}/jobs/job_signature/${jobId}/${nounce}?token=${Deno.env.get("WM_TOKEN")}`)
return {
nounce,
signature: await sig.text()
};
}

export async function getResumeEndpoints() {
const conf = createConf();
const workspace = getWorkspace()

const { nounce, signature } = await genNounceAndHmac(
conf,
workspace,
Deno.env.get("WM_JOB_ID") ?? "no_job_id",
);
const url_prefix = Deno.env.get("WM_BASE_URL") +
`/api/w/${conf.workspace_id}/jobs/`;
`/api/w/${workspace}/jobs/`;

function getResumeUrl(op: string) {
return url_prefix +
Expand Down
Empty file removed deno-client/openapi-bundled.yaml
Empty file.
7 changes: 0 additions & 7 deletions deno-client/openapitools.json

This file was deleted.

5 changes: 5 additions & 0 deletions deno-client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"denoify": {
"index": "index.ts"
}
}
15 changes: 15 additions & 0 deletions deno-client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "es6",
"module": "ES6",
"lib": ["es7", "es6", "dom"],
"declaration": true,
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
"exclude": [
"node_modules",
"dist"
]
}

0 comments on commit 350d31f

Please sign in to comment.