Skip to content

Commit

Permalink
feat(js lib): add format + lint biome + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDecMeetsMore committed Jul 22, 2024
1 parent 4626cf1 commit 6d1bf21
Show file tree
Hide file tree
Showing 32 changed files with 1,371 additions and 1,202 deletions.
14 changes: 13 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
{
"rust-analyzer.check.command": "clippy"
"rust-analyzer.check.command": "clippy",
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true
},
"[typescriptreact]": {
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true
},
"[javascript]": {
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true
}
}
50 changes: 50 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"files": {
"ignore": ["**/swagger.json", "**/package.json"]
},
"organizeImports": {
"enabled": true
},
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true,
"defaultBranch": "master"
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"formatter": {
"enabled": true,
"indentStyle": "space",
"indentWidth": 2
},
"json": {
"parser": {
"allowComments": true,
"allowTrailingCommas": true
},
"formatter": {
"indentStyle": "space",
"indentWidth": 2
}
},
"javascript": {
"parser": {
"unsafeParameterDecoratorsEnabled": true
},
"formatter": {
"indentStyle": "space",
"indentWidth": 2,
"quoteStyle": "single",
"jsxQuoteStyle": "single",
"trailingCommas": "es5",
"semicolons": "asNeeded",
"arrowParentheses": "asNeeded"
}
}
}
44 changes: 22 additions & 22 deletions scheduler/clients/javascript/lib/accountClient.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
import { Account } from "./domain/account";
import { NettuBaseClient } from "./baseClient";
import type { Account } from './domain/account'
import { NettuBaseClient } from './baseClient'

type AccountResponse = {
account: Account;
};
account: Account
}

type CreateAccountResponse = {
account: Account;
secretApiKey: string;
};
account: Account
secretApiKey: string
}

type CreateAccountRequest = {
code: string;
};
code: string
}

type GoogleIntegration = {
clientId: string;
clientSecret: string;
redirectUri: string;
};
clientId: string
clientSecret: string
redirectUri: string
}

export class NettuAccountClient extends NettuBaseClient {
// data will be something in the future
public create(data: CreateAccountRequest) {
return this.post<CreateAccountResponse>("/account", data);
return this.post<CreateAccountResponse>('/account', data)
}

public setPublicSigningKey(publicSigningKey?: string) {
return this.put<AccountResponse>("/account/pubkey", {
return this.put<AccountResponse>('/account/pubkey', {
publicJwtKey: publicSigningKey,
});
})
}

public removePublicSigningKey() {
return this.setPublicSigningKey();
return this.setPublicSigningKey()
}

public setWebhook(url: string) {
return this.put<AccountResponse>(`/account/webhook`, {
return this.put<AccountResponse>('/account/webhook', {
webhookUrl: url,
});
})
}

public connectGoogle(data: GoogleIntegration) {
return this.put<AccountResponse>(`/account/integration/google`, data);
return this.put<AccountResponse>('/account/integration/google', data)
}

public removeWebhook() {
return this.delete<AccountResponse>(`/account/webhook`);
return this.delete<AccountResponse>('/account/webhook')
}

public me() {
return this.get<AccountResponse>(`/account`);
return this.get<AccountResponse>('/account')
}
}
90 changes: 45 additions & 45 deletions scheduler/clients/javascript/lib/baseClient.ts
Original file line number Diff line number Diff line change
@@ -1,122 +1,122 @@
import axios, { AxiosResponse } from "axios";
import { config } from ".";
import axios, { type AxiosResponse } from 'axios'
import { config } from '.'

export abstract class NettuBaseClient {
private readonly credentials: ICredentials;
private readonly credentials: ICredentials

constructor(credentials: ICredentials) {
this.credentials = credentials;
this.credentials = credentials
}

private getAxiosConfig = () => ({
validateStatus: () => true, // allow all status codes without throwing error
headers: this.credentials.createAuthHeaders(),
});
})

protected async get<T>(path: string): Promise<APIResponse<T>> {
const res = await axios.get(config.baseUrl + path, this.getAxiosConfig());
return new APIResponse(res);
const res = await axios.get(config.baseUrl + path, this.getAxiosConfig())
return new APIResponse(res)
}

protected async delete<T>(path: string): Promise<APIResponse<T>> {
const res = await axios.delete(
config.baseUrl + path,
this.getAxiosConfig()
);
return new APIResponse(res);
const res = await axios.delete(config.baseUrl + path, this.getAxiosConfig())
return new APIResponse(res)
}

protected async deleteWithBody<T>(
path: string,
data: any
data: unknown
): Promise<APIResponse<T>> {
const { headers, validateStatus } = this.getAxiosConfig();
const { headers, validateStatus } = this.getAxiosConfig()
const res = await axios({
method: "DELETE",
method: 'DELETE',
data,
url: config.baseUrl + path,
headers,
validateStatus,
});
return new APIResponse(res);
})
return new APIResponse(res)
}

protected async post<T>(path: string, data: any): Promise<APIResponse<T>> {
protected async post<T>(
path: string,
data: unknown
): Promise<APIResponse<T>> {
const res = await axios.post(
config.baseUrl + path,
data,
this.getAxiosConfig()
);
return new APIResponse(res);
)
return new APIResponse(res)
}

protected async put<T>(path: string, data: any): Promise<APIResponse<T>> {
protected async put<T>(path: string, data: unknown): Promise<APIResponse<T>> {
const res = await axios.put(
config.baseUrl + path,
data,
this.getAxiosConfig()
);
return new APIResponse(res);
)
return new APIResponse(res)
}
}

export class APIResponse<T> {
readonly data?: T; // Could be a failed response and therefore nullable
readonly status: number;
readonly res: AxiosResponse;
readonly data?: T // Could be a failed response and therefore nullable
readonly status: number
readonly res: AxiosResponse

constructor(res: AxiosResponse) {
this.res = res;
this.data = res.data;
this.status = res.status;
this.res = res
this.data = res.data
this.status = res.status
}
}

export class UserCreds implements ICredentials {
private readonly nettuAccount: string;
private readonly token?: string;
private readonly nettuAccount: string
private readonly token?: string

constructor(nettuAccount: string, token?: string) {
this.nettuAccount = nettuAccount;
this.token = token;
this.nettuAccount = nettuAccount
this.token = token
}

createAuthHeaders() {
const creds: any = {
"nettu-account": this.nettuAccount,
};
const creds: Record<string, string> = {
'nettu-account': this.nettuAccount,
}
if (this.token) {
creds["authorization"] = `Bearer ${this.token}`;
creds.authorization = `Bearer ${this.token}`
}

return Object.freeze(creds);
return Object.freeze(creds)
}
}

export class AccountCreds implements ICredentials {
private readonly apiKey: string;
private readonly apiKey: string

constructor(apiKey: string) {
this.apiKey = apiKey;
this.apiKey = apiKey
}

createAuthHeaders() {
return Object.freeze({
"x-api-key": this.apiKey,
});
'x-api-key': this.apiKey,
})
}
}

export interface ICredentials {
createAuthHeaders(): object;
createAuthHeaders(): object
}

export class EmptyCreds implements ICredentials {
createAuthHeaders() {
return Object.freeze({});
return Object.freeze({})
}
}

export interface ICredentials {
createAuthHeaders(): object;
createAuthHeaders(): object
}
Loading

0 comments on commit 6d1bf21

Please sign in to comment.