Skip to content

Commit

Permalink
chore: code style
Browse files Browse the repository at this point in the history
  • Loading branch information
mo4islona committed Sep 6, 2022
1 parent 49c1ece commit 6297aa1
Show file tree
Hide file tree
Showing 49 changed files with 8,340 additions and 2,092 deletions.
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# /node_modules/* in the project root is ignored by default
# build artefacts
dist/*
# data definition files
**/*.d.ts
52 changes: 52 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin', 'prettier'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'plugin:import/errors',
'plugin:import/warnings',
'plugin:import/typescript'
],
root: true,
env: {
node: true,
jest: true,
},
rules: {
'max-len': ['error', { code: 120, ignoreComments: true, ignoreStrings: true, ignoreTemplateLiterals: true }],
'prettier/prettier': ['error', { printWidth: 120 }],
'no-implicit-coercion': ['error', { allow: ['!!'] }],
'import/order': [
'error',
{

pathGroups: [
],
'newlines-between': 'always',
alphabetize: {
order: 'asc' /* sort in ascending order. Options: ['ignore', 'asc', 'desc'] */,
caseInsensitive: true /* ignore case. Options: [true, false] */,
},
pathGroupsExcludedImportTypes: ['builtin'],
groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index', 'object'],
},
],
'import/no-unresolved': 'off',
'no-negated-condition': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/no-unused-vars': 'off',
},
};

5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"trailingComma": "all",
"printWidth": 120
}
17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"homepage": "https://www.subsquid.io/",
"scripts": {
"build": "rm -rf lib && tsc",
"dev": "./bin/dev"
"dev": "./bin/dev",
"lint": "eslint --fix src/**/*"
},
"dependencies": {
"@oclif/core": "^1.3.0",
Expand Down Expand Up @@ -61,6 +62,18 @@
"@types/node-fetch": "^2.5.12",
"@types/js-yaml": "^4.0.5",
"@types/ms": "^0.7.31",
"typescript": "~4.5.5"
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/eslint-plugin-tslint": "^5.18.0",
"@typescript-eslint/parser": "^5.19.0",
"typescript": "~4.5.5",
"eslint": "8.22.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^13.0.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"prettier": "^2.6.2"
}
}
54 changes: 32 additions & 22 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,60 @@
import fetch from 'node-fetch';
import path from 'path';

import chalk from 'chalk';
import fetch from 'node-fetch';
import qs from 'query-string';

import { getConfig } from '../config';

const debug = process.env.API_DEBUG === 'true';

let version = 'unknown'
let version = 'unknown';
try {
version = require(path.resolve(__dirname, '../../package.json')).version
// eslint-disable-next-line @typescript-eslint/no-var-requires
version = require(path.resolve(__dirname, '../../package.json')).version;
} catch (e) {}

export class ApiError extends Error {
constructor(public status: number, public body: {
error: string
invalidFields?: { path: string[], message: string, type: string }[]
}) {
constructor(
public status: number,
public body: {
error: string;
invalidFields?: { path: string[]; message: string; type: string }[];
},
) {
super();
}
}

export async function api<T = any>(
{ method, path, data, query, responseType = 'json' } : {
method: 'get' |'post' | 'put' | 'delete' | 'patch'
path: string,
query?: Record<string, string | string[] | boolean | number | undefined>,
data?: unknown
responseType?: 'json' | 'stream'
}
): Promise<{ body: T } > {
export async function api<T = any>({
method,
path,
data,
query,
responseType = 'json',
}: {
method: 'get' | 'post' | 'put' | 'delete' | 'patch';
path: string;
query?: Record<string, string | string[] | boolean | number | undefined>;
data?: unknown;
responseType?: 'json' | 'stream';
}): Promise<{ body: T }> {
const config = getConfig();

const url = `${config.apiUrl}${path}${query ? `?${qs.stringify(query)}` : ''}`
const url = `${config.apiUrl}${path}${query ? `?${qs.stringify(query)}` : ''}`;

const headers = {
'Content-Type': 'application/json',
authorization: `token ${config.credentials}`,
'X-CLI-Version': version,
}
};

if (debug) {
console.log(
chalk.cyan`[HTTP REQUEST]`,
chalk.dim(method?.toUpperCase()),
url,
chalk.dim(JSON.stringify({headers}))
chalk.dim(JSON.stringify({ headers })),
);
if (data) {
console.log(chalk.dim(JSON.stringify(data)));
Expand All @@ -56,17 +66,17 @@ export async function api<T = any>(
body: data ? JSON.stringify(data) : undefined,
});

let body
let body;
try {
body = responseType === 'json'? await response.json() : response.body;
body = responseType === 'json' ? await response.json() : response.body;
} catch (e) {}

if (debug) {
console.log(
chalk.cyan`[HTTP RESPONSE]`,
url,
chalk.cyan(response.status),
chalk.dim(JSON.stringify({ headers: response.headers }))
chalk.dim(JSON.stringify({ headers: response.headers })),
);
if (body && responseType === 'json') {
console.log(chalk.dim(JSON.stringify(body, null, 2)));
Expand Down
8 changes: 4 additions & 4 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './api'
export * from './squids'
export * from './secrets'
export * from './types'
export * from './api';
export * from './squids';
export * from './secrets';
export * from './types';
34 changes: 17 additions & 17 deletions src/api/secrets.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { HttpResponse, SecretsListResponse } from './types';
import { api } from './api';
import { HttpResponse, SecretsListResponse } from './types';

export async function listSecrets(): Promise<SecretsListResponse> {
const { body } = await api<HttpResponse<SecretsListResponse>>( {
method: 'get',
path: '/secrets'
});
return body.payload
const { body } = await api<HttpResponse<SecretsListResponse>>({
method: 'get',
path: '/secrets',
});
return body.payload;
}

export async function removeSecret(name: string): Promise<SecretsListResponse> {
const { body } = await api<HttpResponse<SecretsListResponse>>( {
method: 'delete',
path: `/secrets/${name}`,
});
return body.payload
const { body } = await api<HttpResponse<SecretsListResponse>>({
method: 'delete',
path: `/secrets/${name}`,
});
return body.payload;
}

export async function setSecret(secrets: Record<string, string>): Promise<SecretsListResponse> {
const { body } = await api<HttpResponse<SecretsListResponse>>( {
method: 'patch',
path: `/secrets`,
data: { secrets: secrets }
});
return body.payload
const { body } = await api<HttpResponse<SecretsListResponse>>({
method: 'patch',
path: `/secrets`,
data: { secrets: secrets },
});
return body.payload;
}
Loading

0 comments on commit 6297aa1

Please sign in to comment.