Comparing version 0.0.11 to 1.0.0
import GQLResultInterface, { GQLEdgeInterface, GQLNodeInterface } from "./faces"; | ||
export declare const setEndpointUrl: (full_GQL_Url: string) => string; | ||
export declare const run: (query: string, variables?: Record<string, unknown>, gqlUrl?: string) => Promise<GQLResultInterface>; | ||
export declare const all: (query: string, variables?: Record<string, unknown>, gqlUrl?: string) => Promise<GQLEdgeInterface[]>; | ||
export declare const tx: (id: string, gqlUrl?: string) => Promise<GQLNodeInterface>; | ||
export declare const fetchTxTag: (id: string, name: string, gqlUrl?: string) => Promise<string | undefined>; | ||
export default function arGql(endpointUrl?: string): { | ||
run: (query: string, variables?: Record<string, unknown>) => Promise<GQLResultInterface>; | ||
all: (query: string, variables?: Record<string, unknown>) => Promise<GQLEdgeInterface[]>; | ||
tx: (id: string) => Promise<GQLNodeInterface>; | ||
fetchTxTag: (id: string, name: string) => Promise<string | undefined>; | ||
getConfig: () => { | ||
endpointUrl: string; | ||
}; | ||
}; |
@@ -28,3 +28,3 @@ "use strict"; | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
while (g && (g = 0, op[0] && (_ = 0)), _) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
@@ -51,92 +51,103 @@ if (y = 0, t) op = [op[0] & 2, t.value]; | ||
exports.__esModule = true; | ||
exports.fetchTxTag = exports.tx = exports.all = exports.run = exports.setEndpointUrl = void 0; | ||
var axios_1 = require("axios"); | ||
var tx_1 = require("./queries/tx"); | ||
var GQL_ENDPOINT = "https://arweave.net/graphql"; //default | ||
var setEndpointUrl = function (full_GQL_Url) { return GQL_ENDPOINT = full_GQL_Url; }; | ||
exports.setEndpointUrl = setEndpointUrl; | ||
var run = function (query, variables, gqlUrl) { return __awaiter(void 0, void 0, void 0, function () { | ||
var graphql, res; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
graphql = JSON.stringify({ | ||
query: query, | ||
variables: variables | ||
}); | ||
return [4 /*yield*/, axios_1["default"].post(gqlUrl || GQL_ENDPOINT, graphql, { | ||
headers: { | ||
"content-type": "application/json" | ||
} | ||
})]; | ||
case 1: | ||
res = (_a.sent()).data; | ||
return [2 /*return*/, res]; | ||
} | ||
}); | ||
}); }; | ||
exports.run = run; | ||
var all = function (query, variables, gqlUrl) { return __awaiter(void 0, void 0, void 0, function () { | ||
var hasNextPage, edges, cursor, res; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
hasNextPage = true; | ||
edges = []; | ||
cursor = ""; | ||
_a.label = 1; | ||
case 1: | ||
if (!hasNextPage) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, (0, exports.run)(query, __assign(__assign({}, variables), { cursor: cursor }), gqlUrl)]; | ||
case 2: | ||
res = (_a.sent()).data.transactions; | ||
if (res.edges && res.edges.length) { | ||
edges = edges.concat(res.edges); | ||
cursor = res.edges[res.edges.length - 1].cursor; | ||
} | ||
hasNextPage = res.pageInfo.hasNextPage; | ||
return [3 /*break*/, 1]; | ||
case 3: return [2 /*return*/, edges]; | ||
} | ||
}); | ||
}); }; | ||
exports.all = all; | ||
var tx = function (id, gqlUrl) { return __awaiter(void 0, void 0, void 0, function () { | ||
var isBrowser, cache, res, cache; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
isBrowser = typeof window !== "undefined"; | ||
if (isBrowser) { | ||
cache = JSON.parse(localStorage.getItem("gqlCache") || "{}"); | ||
if (id in cache) | ||
return [2 /*return*/, JSON.parse(cache[id])]; | ||
} | ||
return [4 /*yield*/, (0, exports.run)(tx_1["default"], { id: id }, gqlUrl)]; | ||
case 1: | ||
res = _a.sent(); | ||
if (isBrowser && res.data.transaction.block) { | ||
cache = JSON.parse(localStorage.getItem("gqlCache") || "{}"); | ||
cache[id] = res.data.transaction; | ||
localStorage.setItem("gqlCache", JSON.stringify(cache)); | ||
} | ||
return [2 /*return*/, res.data.transaction]; | ||
} | ||
}); | ||
}); }; | ||
exports.tx = tx; | ||
var fetchTxTag = function (id, name, gqlUrl) { return __awaiter(void 0, void 0, void 0, function () { | ||
var res, tag; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, (0, exports.tx)(id, gqlUrl)]; | ||
case 1: | ||
res = _a.sent(); | ||
tag = res.tags.find(function (tag) { return tag.name === name; }); | ||
if (tag) | ||
return [2 /*return*/, tag.value]; | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
exports.fetchTxTag = fetchTxTag; | ||
function arGql(endpointUrl) { | ||
var _this = this; | ||
//sanity check | ||
if (endpointUrl && !endpointUrl.match(/^https?:\/\/.*\/graphql$/)) { | ||
throw new Error("string doesn't appear to be a URL of the form <http(s)://some-domain/graphql>'. You entered \"".concat(endpointUrl, "\"")); | ||
} | ||
var _endpointUrl = endpointUrl || 'https://arweave.net/graphql'; //default to arweave.net/graphql | ||
var run = function (query, variables) { return __awaiter(_this, void 0, void 0, function () { | ||
var graphql, res; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
graphql = JSON.stringify({ | ||
query: query, | ||
variables: variables | ||
}); | ||
return [4 /*yield*/, fetch(_endpointUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
}, | ||
body: graphql | ||
})]; | ||
case 1: | ||
res = _a.sent(); | ||
if (!res.ok) { | ||
throw new Error("(".concat(res.status, ") ").concat(res.statusText)); | ||
} | ||
return [4 /*yield*/, res.json()]; | ||
case 2: return [2 /*return*/, _a.sent()]; | ||
} | ||
}); | ||
}); }; | ||
var all = function (query, variables) { return __awaiter(_this, void 0, void 0, function () { | ||
var hasNextPage, edges, cursor, res; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
hasNextPage = true; | ||
edges = []; | ||
cursor = ""; | ||
_a.label = 1; | ||
case 1: | ||
if (!hasNextPage) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, run(query, __assign(__assign({}, variables), { cursor: cursor }))]; | ||
case 2: | ||
res = (_a.sent()).data.transactions; | ||
if (res.edges && res.edges.length) { | ||
edges = edges.concat(res.edges); | ||
cursor = res.edges[res.edges.length - 1].cursor; | ||
} | ||
hasNextPage = res.pageInfo.hasNextPage; | ||
return [3 /*break*/, 1]; | ||
case 3: return [2 /*return*/, edges]; | ||
} | ||
}); | ||
}); }; | ||
var tx = function (id) { return __awaiter(_this, void 0, void 0, function () { | ||
var isBrowser, res; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
isBrowser = typeof window !== "undefined"; | ||
return [4 /*yield*/, run(tx_1["default"], { id: id })]; | ||
case 1: | ||
res = _a.sent(); | ||
// if (isBrowser && res.data.transaction.block) { | ||
// const cache = JSON.parse(localStorage.getItem("gqlCache") || "{}"); | ||
// cache[id] = res.data.transaction; | ||
// localStorage.setItem("gqlCache", JSON.stringify(cache)); | ||
// } | ||
return [2 /*return*/, res.data.transaction]; | ||
} | ||
}); | ||
}); }; | ||
var fetchTxTag = function (id, name) { return __awaiter(_this, void 0, void 0, function () { | ||
var res, tag; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, tx(id)]; | ||
case 1: | ||
res = _a.sent(); | ||
tag = res.tags.find(function (tag) { return tag.name === name; }); | ||
if (tag) | ||
return [2 /*return*/, tag.value]; | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
return { | ||
run: run, | ||
all: all, | ||
tx: tx, | ||
fetchTxTag: fetchTxTag, | ||
getConfig: function () { return ({ | ||
endpointUrl: _endpointUrl | ||
}); } | ||
}; | ||
} | ||
exports["default"] = arGql; |
{ | ||
"name": "ar-gql", | ||
"version": "0.0.11", | ||
"version": "1.0.0", | ||
"main": "dist/index", | ||
"types": "dist/index", | ||
"author": "John Letey <johnletey@gmail.com>", | ||
@@ -12,15 +14,29 @@ "contributors": [ | ||
"scripts": { | ||
"build": "npx rimraf dist && tsc", | ||
"build": "rimraf dist && tsc", | ||
"postversion": "git push && git push --tags && npm run build && npm publish", | ||
"test": "mocha **/*.spec.ts", | ||
"fmt": "prettier --write ." | ||
}, | ||
"main": "dist/index", | ||
"types": "dist/index", | ||
"dependencies": { | ||
"axios": "^0.27.2" | ||
"mocha": { | ||
"extension": [ | ||
"ts" | ||
], | ||
"node-option": [ | ||
"require=ts-node/register", | ||
"no-warnings" | ||
] | ||
}, | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.3.4", | ||
"@types/mocha": "^10.0.1", | ||
"chai": "^4.3.7", | ||
"mocha": "^10.2.0", | ||
"prettier": "^2.2.1", | ||
"typescript": "^4.2.3" | ||
"rimraf": "^4.1.2", | ||
"ts-node": "latest", | ||
"typescript": "latest" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# `ar-gql` | ||
# `ar-gql` version 1 | ||
@@ -16,2 +16,24 @@ [](https://www.npmjs.com/package/ar-gql) | ||
## Code Set Up | ||
```ts | ||
import arGql from 'ar-gql' | ||
//... | ||
const argql = arGql() // default is `https://arweave.net/graphql`. | ||
// you can now use argql similar to as before and it will make requests to the default GQL endpoint | ||
const tx = await argql.tx('DeYQPjoEQLLds7usOMZFJFCe7VyTpodYl6Mok6UP6Z4') | ||
console.log(tx.id) // 'DeYQPjoEQLLds7usOMZFJFCe7VyTpodYl6Mok6UP6Z4' | ||
// you can set up another instance with another enpoint | ||
const goldsky = arGql('https://arweave-search.goldsky.com/graphql') | ||
// and use it at the same time | ||
const edges = await goldsky.tx(someTxid) | ||
//... | ||
``` | ||
## Functions | ||
@@ -64,4 +86,4 @@ | ||
### `setEndpointUrl(GQL_URL)` | ||
### `getConfig()` | ||
Set the GQL endpoint. Defaults to `https://arweave.net/graphql`. | ||
Currently returns an object with a single field `endpointUrl` the GQL endpoint of the instance. |
164
src/index.ts
@@ -5,87 +5,101 @@ import GQLResultInterface, { | ||
} from "./faces"; | ||
import axios from "axios"; | ||
import txQuery from "./queries/tx"; | ||
let GQL_ENDPOINT = "https://arweave.net/graphql" //default | ||
export const setEndpointUrl = (full_GQL_Url: string) => GQL_ENDPOINT = full_GQL_Url | ||
export default function arGql(endpointUrl?: string){ | ||
//sanity check | ||
if(endpointUrl && !endpointUrl.match(/^https?:\/\/.*\/graphql$/)){ | ||
throw new Error(`string doesn't appear to be a URL of the form <http(s)://some-domain/graphql>'. You entered "${endpointUrl}"`) | ||
} | ||
const _endpointUrl = endpointUrl || 'https://arweave.net/graphql' //default to arweave.net/graphql | ||
export const run = async ( | ||
query: string, | ||
variables?: Record<string, unknown>, | ||
gqlUrl?: string, | ||
): Promise<GQLResultInterface> => { | ||
const graphql = JSON.stringify({ | ||
query, | ||
variables, | ||
}); | ||
const { data: res } = await axios.post( | ||
gqlUrl || GQL_ENDPOINT, | ||
graphql, | ||
{ | ||
const run = async ( | ||
query: string, | ||
variables?: Record<string, unknown>, | ||
): Promise<GQLResultInterface> => { | ||
const graphql = JSON.stringify({ | ||
query, | ||
variables, | ||
}); | ||
const res = await fetch(_endpointUrl, { | ||
method: 'POST', | ||
headers: { | ||
"content-type": "application/json", | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: graphql, | ||
}); | ||
if(!res.ok){ | ||
throw new Error(`(${res.status}) ${res.statusText}`) | ||
} | ||
); | ||
return await res.json(); | ||
}; | ||
return res; | ||
}; | ||
export const all = async ( | ||
query: string, | ||
variables?: Record<string, unknown>, | ||
gqlUrl?: string, | ||
): Promise<GQLEdgeInterface[]> => { | ||
let hasNextPage = true; | ||
let edges: GQLEdgeInterface[] = []; | ||
let cursor: string = ""; | ||
while (hasNextPage) { | ||
const res = ( | ||
await run( | ||
query, | ||
{ ...variables, cursor }, | ||
gqlUrl, | ||
) | ||
).data.transactions; | ||
if (res.edges && res.edges.length) { | ||
edges = edges.concat(res.edges); | ||
cursor = res.edges[res.edges.length - 1].cursor; | ||
const all = async ( | ||
query: string, | ||
variables?: Record<string, unknown>, | ||
): Promise<GQLEdgeInterface[]> => { | ||
let hasNextPage = true; | ||
let edges: GQLEdgeInterface[] = []; | ||
let cursor: string = ""; | ||
while (hasNextPage) { | ||
const res = ( | ||
await run( | ||
query, | ||
{ ...variables, cursor }, | ||
) | ||
).data.transactions; | ||
if (res.edges && res.edges.length) { | ||
edges = edges.concat(res.edges); | ||
cursor = res.edges[res.edges.length - 1].cursor; | ||
} | ||
hasNextPage = res.pageInfo.hasNextPage; | ||
} | ||
hasNextPage = res.pageInfo.hasNextPage; | ||
} | ||
return edges; | ||
}; | ||
return edges; | ||
}; | ||
const tx = async (id: string): Promise<GQLNodeInterface> => { | ||
const isBrowser: boolean = typeof window !== "undefined"; | ||
// if (isBrowser) { | ||
// const cache = JSON.parse(localStorage.getItem("gqlCache") || "{}"); | ||
// if (id in cache) return JSON.parse(cache[id]); | ||
// } | ||
const res = await run(txQuery, { id }); | ||
// if (isBrowser && res.data.transaction.block) { | ||
// const cache = JSON.parse(localStorage.getItem("gqlCache") || "{}"); | ||
// cache[id] = res.data.transaction; | ||
// localStorage.setItem("gqlCache", JSON.stringify(cache)); | ||
// } | ||
return res.data.transaction; | ||
}; | ||
const fetchTxTag = async ( | ||
id: string, | ||
name: string, | ||
): Promise<string | undefined> => { | ||
const res = await tx(id); | ||
export const tx = async (id: string, gqlUrl?: string): Promise<GQLNodeInterface> => { | ||
const isBrowser: boolean = typeof window !== "undefined"; | ||
if (isBrowser) { | ||
const cache = JSON.parse(localStorage.getItem("gqlCache") || "{}"); | ||
if (id in cache) return JSON.parse(cache[id]); | ||
const tag = res.tags.find((tag) => tag.name === name); | ||
if (tag) return tag.value; | ||
}; | ||
return { | ||
run, | ||
all, | ||
tx, | ||
fetchTxTag, | ||
getConfig: ()=> ({ | ||
endpointUrl: _endpointUrl, | ||
}) | ||
} | ||
const res = await run(txQuery, { id }, gqlUrl); | ||
if (isBrowser && res.data.transaction.block) { | ||
const cache = JSON.parse(localStorage.getItem("gqlCache") || "{}"); | ||
cache[id] = res.data.transaction; | ||
localStorage.setItem("gqlCache", JSON.stringify(cache)); | ||
} | ||
return res.data.transaction; | ||
}; | ||
export const fetchTxTag = async ( | ||
id: string, | ||
name: string, | ||
gqlUrl?: string | ||
): Promise<string | undefined> => { | ||
const res = await tx(id, gqlUrl); | ||
const tag = res.tags.find((tag) => tag.name === name); | ||
if (tag) return tag.value; | ||
}; | ||
} |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
19844
0
14
477
1
88
8
1
- Removedaxios@^0.27.2
- Removedasynckit@0.4.0(transitive)
- Removedaxios@0.27.2(transitive)
- Removedcall-bind-apply-helpers@1.0.2(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removeddunder-proto@1.0.1(transitive)
- Removedes-define-property@1.0.1(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-object-atoms@1.1.1(transitive)
- Removedes-set-tostringtag@2.1.0(transitive)
- Removedfollow-redirects@1.15.9(transitive)
- Removedform-data@4.0.2(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-intrinsic@1.3.0(transitive)
- Removedget-proto@1.0.1(transitive)
- Removedgopd@1.2.0(transitive)
- Removedhas-symbols@1.1.0(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedmath-intrinsics@1.1.0(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)