Comparing version
@@ -1,7 +0,7 @@ | ||
import * as axios from "axios"; | ||
import { IQuery } from "../types/abstract/IQuery"; | ||
import { IEntity } from "../types/abstract/IEntity"; | ||
import { Method } from "../types/http/method"; | ||
interface IFetchSqlApiProps<Query extends IQuery> { | ||
path: string; | ||
method: axios.Method; | ||
method: Method; | ||
body?: Query; | ||
@@ -29,3 +29,7 @@ params: { | ||
*/ | ||
export declare function fetchJsqlApi<Query extends IQuery, Table extends IEntity = any>({ path, method, body, params, headers, isLocal }: IFetchSqlApiProps<Query>): Promise<axios.AxiosResponse<Table, any>>; | ||
export declare function fetchJsqlApi<Query extends IQuery, Table extends IEntity = any>({ path, method, body, params, headers, isLocal }: IFetchSqlApiProps<Query>): Promise<{ | ||
data: Table; | ||
status: number; | ||
headers: Headers; | ||
}>; | ||
export {}; |
@@ -1,3 +0,1 @@ | ||
import * as axios from "axios"; | ||
import { isAxiosError } from "axios"; | ||
/** | ||
@@ -13,24 +11,35 @@ * Make a query to the database | ||
*/ | ||
export async function fetchJsqlApi({ path, method = 'GET', body, params, headers = { | ||
export async function fetchJsqlApi({ path, method = 'get', body, params, headers = { | ||
'Content-Type': 'application/json' | ||
}, isLocal }) { | ||
const config = { | ||
const baseUrl = `http${isLocal ? '' : 's'}://${isLocal ? `localhost:${isLocal}` : 'jsql-api'}/${path}`; | ||
const url = new URL(baseUrl); | ||
Object.entries(params).forEach(([key, value]) => { | ||
if (value !== undefined) { | ||
url.searchParams.append(key, value); | ||
} | ||
}); | ||
const fetchOptions = { | ||
method, | ||
url: `http${isLocal ? '' : 's'}://${isLocal ? `localhost:${isLocal}` : 'jsql-api'}/${path}`, | ||
headers: headers, | ||
params: params, | ||
data: body | ||
headers, | ||
body: method !== 'get' && body ? JSON.stringify(body) : undefined | ||
}; | ||
try { | ||
const response = await axios.default(config); | ||
return response; | ||
const response = await fetch(url.toString(), fetchOptions); | ||
if (!response.ok) { | ||
const errorBody = await response.text(); | ||
console.error("Jsql Api Error:", errorBody); | ||
throw new Error(`Error ${response.status}: ${response.statusText}`); | ||
} | ||
const data = await response.json(); | ||
return { | ||
data, | ||
status: response.status, | ||
headers: response.headers | ||
}; | ||
} | ||
catch (error) { | ||
if (isAxiosError(error)) { | ||
console.error("Jsql Api Error:", error.response?.data || error.message); | ||
throw error; | ||
} | ||
console.error('Jsql Api Unknown Error:', error); | ||
console.error("Jsql Api Unknown Error:", error.message || error); | ||
throw error; | ||
} | ||
} |
{ | ||
"name": "jsql-api", | ||
"version": "0.0.55", | ||
"version": "0.0.56", | ||
"main": "dist/index.d.ts", | ||
@@ -25,3 +25,2 @@ "types": "dist/index.d.ts", | ||
"dependencies": { | ||
"axios": "^1.9.0", | ||
"typescript": "^5.8.3" | ||
@@ -28,0 +27,0 @@ }, |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
12099
4.18%1
-50%74
2.78%331
4.75%1
Infinity%- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed