Skip to content

Commit a2eebca

Browse files
committed
fix: unwrap some api features
1 parent 4ad2ee6 commit a2eebca

File tree

13 files changed

+306
-56
lines changed

13 files changed

+306
-56
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"axios": "^1.2.3",
1919
"fs-extra": "^11.1.0",
2020
"handlebars": "^4.7.7",
21+
"pino": "^8.11.0",
22+
"pino-pretty": "^9.4.0",
2123
"promisify-child-process": "^4.1.1",
2224
"prompts": "^2.4.2",
2325
"tslib": "^2.4.1",
Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios, { AxiosError, AxiosInstance, AxiosResponse } from "axios";
2-
import { AuthStrategy } from "./types";
2+
import { prepare_client, FaableClientConfig } from "./client";
33
export interface FaableApp {
44
id: string;
55
name: string;
@@ -20,7 +20,11 @@ const wrap_error = async <T>(prom: Promise<AxiosResponse<T>>): Promise<T> => {
2020
const e: AxiosError<{ message: string }> = error;
2121
if (e.isAxiosError) {
2222
const res = e.response;
23-
throw new Error(`API Error: ${res.data.message}`);
23+
if (res) {
24+
throw new Error(`API Error ${res.status}: ${res?.data.message}`);
25+
} else {
26+
throw new Error(`API Error:${e.message}`);
27+
}
2428
}
2529
throw error;
2630
}
@@ -35,37 +39,17 @@ const paginate = async <Q extends Promise<Page<T>>, T>(
3539
return items;
3640
};
3741

38-
type FaableApiConfig<T = any> = {
39-
authStrategy?: AuthStrategy<T>;
40-
auth?: T;
41-
};
42-
export class FaableAppsApi {
42+
type FaableApiConfig<T> = {} & FaableClientConfig<T>;
43+
44+
export class FaableApi<T = any> {
4345
client: AxiosInstance;
4446

45-
constructor(config: FaableApiConfig) {
46-
const { authStrategy, auth } = config;
47-
const strategy = authStrategy && authStrategy(auth);
48-
this.client = axios.create({
49-
baseURL: "https://api.faable.com",
50-
});
51-
this.client.interceptors.request.use(
52-
async function (config) {
53-
// Do something before request is sent
54-
const headers = strategy ? await strategy.headers() : {};
55-
config.headers.set(headers);
56-
// console.log("all headers");
57-
// console.log(headers);
58-
return config;
59-
},
60-
function (error) {
61-
// Do something with request error
62-
return Promise.reject(error);
63-
}
64-
);
47+
constructor(config: FaableApiConfig<T>) {
48+
this.client = prepare_client(config);
6549
}
6650

67-
static create(config: FaableApiConfig = {}) {
68-
return new FaableAppsApi(config);
51+
static create<T>(config: FaableApiConfig<T> = {}) {
52+
return new FaableApi(config);
6953
}
7054

7155
async list() {

src/api/authenticateOAuthApp.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import axios from "axios";
2-
import { config } from "./config";
3-
import { AuthStrategy } from "./types";
2+
import { AuthStrategyBuilder } from "./types";
43

54
interface ClientCredentials {
65
clientId: string;
@@ -13,9 +12,9 @@ type TokenResponse = {
1312
token_type: string;
1413
};
1514

16-
export const authenticateOAuthApp: AuthStrategy<Partial<ClientCredentials>> = (
17-
creds = config
18-
) => {
15+
export const authenticateOAuthApp: AuthStrategyBuilder<
16+
Partial<ClientCredentials>
17+
> = (creds) => {
1918
const { clientId, clientSecret } = creds;
2019
if (!clientId || !clientSecret) {
2120
throw new Error("Missing credentials. Run faable configure first.");

src/api/client.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import axios from "axios";
2+
import { AuthStrategy, AuthStrategyBuilder } from "./types";
3+
4+
export type FaableClientConfig<T = any> = {
5+
authStrategy?: AuthStrategyBuilder<T>;
6+
auth?: T;
7+
};
8+
9+
export function prepare_client<T>({
10+
authStrategy,
11+
auth,
12+
}: FaableClientConfig<T> = {}) {
13+
const strategy: AuthStrategy | undefined = authStrategy && authStrategy(auth);
14+
const client = axios.create({
15+
baseURL: "https://api.faable.com",
16+
timeout: 5000,
17+
});
18+
19+
client.interceptors.request.use(
20+
async function (config) {
21+
// Do something before request is sent
22+
const headers = strategy ? await strategy.headers() : {};
23+
config.headers.set(headers);
24+
// console.log("all headers");
25+
// console.log(headers);
26+
return config;
27+
},
28+
function (error) {
29+
// Do something with request error
30+
return Promise.reject(error);
31+
}
32+
);
33+
return client;
34+
}

src/api/context.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { FaableAppsApi } from "./faable_api";
2-
1+
import { FaableApi } from "./FaableApi";
2+
import { authenticateOAuthApp } from "./authenticateOAuthApp";
33
export const context = async () => {
4-
const strategy = (await import("./authenticateOAuthApp"))
5-
.authenticateOAuthApp;
4+
const authStrategy = authenticateOAuthApp;
5+
const { config } = await import("./userdir_config");
66
return {
7-
api: FaableAppsApi.create({ authStrategy: strategy }),
7+
api: FaableApi.create({ authStrategy, auth: config }),
88
};
99
};

src/api/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type AuthStrategyResponse = {
1+
export type AuthStrategy = {
22
headers: () => Promise<Record<string, string>>;
33
};
4-
export type AuthStrategy<T> = (...params: T[]) => AuthStrategyResponse;
4+
export type AuthStrategyBuilder<T> = (...params: T[]) => AuthStrategy;

src/commands/configure/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { CommandModule } from "yargs";
22
import prompts from "prompts";
3-
import { FaableAppsApi } from "../../api/faable_api";
3+
import { FaableApi } from "../../api/FaableApi";
44
import fs from "fs-extra";
55
import os from "os";
66
import path from "path";
77
type Options = {
8-
api: FaableAppsApi;
8+
api: FaableApi;
99
app_name: string;
1010
workdir: string;
1111
remove: boolean;

src/commands/deploy/analyze_package.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ interface AnalyzePackage {
99

1010
export const analyze_package = async ({ workdir }: AnalyzePackage) => {
1111
const package_file = path.join(path.resolve(workdir), "package.json");
12-
log.info(`Reading ${package_file}`);
12+
log.info(`Loading config from package.json...`);
1313
const pkg: PackageJson = await fs.readJSON(package_file);
1414

1515
// Package must have a start script
1616
if (!pkg?.scripts?.start) {
17-
throw new Error("Missing start script on package.json");
17+
throw new Error("Missing start script");
1818
}
1919

2020
// Check if build is required to run
2121
let hasBuild = false;
2222
if (pkg?.scripts?.build) {
23-
log.info(`Build script found`);
23+
log.info(`Build step: ${pkg?.scripts?.build}`);
2424
hasBuild = true;
2525
} else {
2626
log.info(`No build script found`);

src/commands/deploy/upload_tag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FaableAppRegistry } from "../../api/faable_api";
1+
import { FaableAppRegistry } from "../../api/FaableApi";
22
import { log } from "../../log";
33
import { cmd } from "./cmd";
44

0 commit comments

Comments
 (0)