Skip to content

Commit

Permalink
feat(chore): .npmrc if nof exists, improve telegram auth
Browse files Browse the repository at this point in the history
  • Loading branch information
anteqkois committed May 30, 2024
1 parent 6d28856 commit 67d3a4f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 41 deletions.
19 changes: 12 additions & 7 deletions libs/connectors/common/src/lib/http/core/http-error.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { AxiosError } from 'axios'

export class HttpError extends Error {
constructor(private readonly _requestBody: unknown, private readonly _err: AxiosError) {
constructor(private readonly _requestBody: unknown, readonly axiosError: AxiosError) {
super(
JSON.stringify({
response: {
status: _err?.response?.status || 500,
body: _err?.response?.data,
status: axiosError?.response?.status || 500,
body: axiosError?.response?.data,
},
request: {
body: _requestBody,
Expand All @@ -18,8 +18,8 @@ export class HttpError extends Error {
public errorMessage() {
return {
response: {
status: this._err?.response?.status || 500,
body: this._err?.response?.data,
status: this.axiosError?.response?.status || 500,
body: this.axiosError?.response?.data,
},
request: {
body: this._requestBody,
Expand All @@ -29,8 +29,8 @@ export class HttpError extends Error {

get response() {
return {
status: this._err?.response?.status || 500,
body: this._err?.response?.data,
status: this.axiosError?.response?.status || 500,
body: this.axiosError?.response?.data,
}
}

Expand All @@ -39,4 +39,9 @@ export class HttpError extends Error {
body: this._requestBody,
}
}

static isHttpError(error: unknown): error is HttpError {
if (error && typeof error === 'object' && 'axiosError' in error) return true
return false
}
}
10 changes: 5 additions & 5 deletions libs/connectors/telegram-bot/src/common/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpMethod, HttpRequest, httpClient } from '@linkerry/connectors-common'
import { HttpError, HttpMethod, HttpRequest, httpClient } from '@linkerry/connectors-common'
import { ConnectorAuth } from '@linkerry/connectors-framework'
import { HttpStatusCode, isAxiosError } from 'axios'
import { HttpStatusCode } from 'axios'
import { GetMe } from '../types/getMe'
import { telegramCommons } from './common'

Expand Down Expand Up @@ -37,16 +37,16 @@ export const telegramBotAuth = ConnectorAuth.SecretText({
error: 'Invalid Bot Token',
}
} catch (error: any) {
if (isAxiosError(error)) {
if (error.status === HttpStatusCode.NotFound)
if (HttpError.isHttpError(error)) {
if (error.axiosError.status === HttpStatusCode.NotFound)
return {
valid: false,
error: 'Invalid Bot Token, bot not found',
}
else
return {
valid: false,
error: error.response?.data?.description,
error: error.axiosError?.response?.data,
}
}

Expand Down
18 changes: 15 additions & 3 deletions libs/nest-core/src/lib/package-manager/package-manager.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { isEmpty } from '@linkerry/shared'
import { Logger } from '@nestjs/common'
import { exec as execCallback } from 'node:child_process'
import { writeFile } from 'node:fs/promises'
import { access, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { promisify } from 'node:util'

const npmrcContent = `
strict-peer-dependencies=false
auto-install-peers=true
ignore-scripts=true
registry=\${REGISTRY_USERNAME}:\${REGISTRY_PASSWORD}@\${REGISTRY_URL}
registry=\${REGISTRY_URL}
//\${REGISTRY_URL}/:_authToken=\${REGISTRY_TOKEN}
`
Expand Down Expand Up @@ -38,13 +37,26 @@ export type PackageInfo = {
spec: string
}

const writeNpmrcIfNotExists = async (path: string, npmrcContent: string) => {
const filePath = join(path, '.npmrc');

try {
await access(filePath);
// logger.debug(`File ${filePath} already exists. Skipping write operation.`);
} catch (error) {
await writeFile(filePath, npmrcContent.trim());
// logger.debug(`File ${filePath} has been created.`);
}
};

const runCommand = async (path: string, command: Command, ...args: string[]): Promise<PackageManagerOutput> => {
const commandLine = `pnpm ${command} ${args.join(' ')}`
try {
logger.debug(`#runCommand:`, { commandLine, path })
return await exec(commandLine, { cwd: path })
} catch (error: any) {
logger.error(error)
logger.error(error?.stdout)
throw new Error(error)
}
}
Expand All @@ -65,7 +77,7 @@ export const packageManager = {
},

async init({ path }: InitParams): Promise<PackageManagerOutput> {
await writeFile(join(path, '.npmrc'), npmrcContent.trim())
await writeNpmrcIfNotExists(path, npmrcContent)

return runCommand(path, 'init')
},
Expand Down
60 changes: 34 additions & 26 deletions tools/scripts/connectors/telegram/validToken.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
import axios, { isAxiosError } from 'axios'
import { HttpStatusCode } from 'axios'
import { HttpError, HttpMethod, HttpRequest, httpClient } from '../../../../libs/connectors/common/src/lib/http'
// import { HttpMethod, HttpRequest, httpClient } from '@linkerry/connectors-common'

const main = async () => {
try {
// await axios.get('https://api.telegram.org/bot523444/getMe')
await axios.get('https://api.telegram.org/bot523444/getMe', {
params: {
siema: 1,
},
})
// await axios.post('https://api.telegram.org/bot523444/getMe', {
// siema: 1,
// siema2: 2
// })
} catch (error) {
if (isAxiosError(error)) {
console.log(error.response.headers)
const botToken = 'bot423423'
const methodName = 'getMe'

const request: HttpRequest = {
method: HttpMethod.GET,
url: `https://api.telegram.org/bot${botToken}/${methodName}`,
}

// console.log(error.config.headers)
// console.log(error.config.params)
// console.log(error.config.method)
// console.log(error.config.url)
// console.log(error.config.data)
try {
const response = await httpClient.sendRequest(request)

// console.log(error.request._header)
// console.log(error.request.protocol)
// console.log(error.request.host)
// console.log(error.request.path)
if (response.body.ok)
return {
valid: true,
}
else
return {
valid: true,
error: 'Invalid Bot Token',
}
} catch (error: any) {
if (HttpError.isHttpError(error)) {
if (error.axiosError.status === HttpStatusCode.NotFound)
return {
valid: false,
error: 'Invalid Bot Token, bot not found',
}
else
return {
valid: false,
error: error.axiosError.response.data,
}
}
}

process.exit(0)
}
}

main()
Expand Down

0 comments on commit 67d3a4f

Please sign in to comment.