minifetch-api 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.env-local ADDED
@@ -0,0 +1,10 @@
1
+ NETWORK=base-sepolia
2
+ EXPLORER_BASE_URL=https://sepolia.basescan.org
3
+
4
+ API_SERVER_URL=http://localhost:4021
5
+ CHECK_ROBOTS_API_PATH=/api/v1/free/preflight/url-check
6
+ METADATA_API_PATH=/api/v1/x402/extract/url-metadata
7
+
8
+ METADATA_URL=
9
+ METADATA_API_PARAM_INCLUDE_RESPONSE_BODY=
10
+ PRIVATE_KEY=
@@ -0,0 +1,8 @@
1
+ docs/
2
+ dist/
3
+ node_modules/
4
+ coverage/
5
+ .github/
6
+ src/client
7
+ **/**/*.json
8
+ *.md
package/.prettierrc ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "tabWidth": 2,
3
+ "useTabs": false,
4
+ "semi": true,
5
+ "singleQuote": false,
6
+ "trailingComma": "all",
7
+ "bracketSpacing": true,
8
+ "arrowParens": "avoid",
9
+ "printWidth": 100,
10
+ "proseWrap": "never"
11
+ }
package/.tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ES2020",
5
+ "moduleResolution": "bundler",
6
+ "esModuleInterop": true,
7
+ "forceConsistentCasingInFileNames": true,
8
+ "skipLibCheck": true,
9
+ "strict": true,
10
+ "resolveJsonModule": true,
11
+ "baseUrl": ".",
12
+ "types": ["node"]
13
+ },
14
+ "include": ["index.ts"]
15
+ }
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # Minifetch.com API
2
+
3
+ Works with x402 payments on Base Sepolia Testnet (only) so far. Testnet API endpoints are not exposed. Check back again later!
4
+
5
+ This client based on x402 payments [example client code](https://github.com/coinbase/x402/blob/main/examples/typescript/clients/fetch).
6
+
7
+ ## Prerequisites
8
+
9
+ - Node.js v20+ (install via [nvm](https://github.com/nvm-sh/nvm))
10
+ - pnpm v10 (install via [pnpm.io/installation](https://pnpm.io/installation))
11
+ - A valid Ethereum private key for making USDC payments on Base Sepolia Testnet (only) -- live production client coming soon!
12
+
13
+ ## Setup
14
+
15
+ 1. Copy `.env-local` to `.env-base-testnet` and add your Ethereum private key:
16
+ ```bash
17
+ cp .env-local .env-base-testnet
18
+ ```
19
+
20
+ 2. Start the example client:
21
+ ```bash
22
+ pnpm install
23
+ pnpm dev:testnet:check
24
+ pnpm dev:testnet
25
+ ```
26
+
@@ -0,0 +1,72 @@
1
+ import js from "@eslint/js";
2
+ import ts from "@typescript-eslint/eslint-plugin";
3
+ import tsParser from "@typescript-eslint/parser";
4
+ import prettier from "eslint-plugin-prettier";
5
+ import jsdoc from "eslint-plugin-jsdoc";
6
+ import importPlugin from "eslint-plugin-import";
7
+
8
+ export default [
9
+ {
10
+ ignores: ["dist/**", "node_modules/**"],
11
+ },
12
+ {
13
+ files: ["**/*.ts"],
14
+ languageOptions: {
15
+ parser: tsParser,
16
+ sourceType: "module",
17
+ ecmaVersion: 2020,
18
+ globals: {
19
+ process: "readonly",
20
+ __dirname: "readonly",
21
+ module: "readonly",
22
+ require: "readonly",
23
+ Buffer: "readonly",
24
+ exports: "readonly",
25
+ setTimeout: "readonly",
26
+ clearTimeout: "readonly",
27
+ setInterval: "readonly",
28
+ clearInterval: "readonly",
29
+ },
30
+ },
31
+ plugins: {
32
+ "@typescript-eslint": ts,
33
+ prettier: prettier,
34
+ jsdoc: jsdoc,
35
+ import: importPlugin,
36
+ },
37
+ rules: {
38
+ ...ts.configs.recommended.rules,
39
+ "import/first": "error",
40
+ "prettier/prettier": "error",
41
+ "@typescript-eslint/member-ordering": "error",
42
+ "@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_$" }],
43
+ "jsdoc/tag-lines": ["error", "any", { startLines: 1 }],
44
+ "jsdoc/check-alignment": "error",
45
+ "jsdoc/no-undefined-types": "off",
46
+ "jsdoc/check-param-names": "error",
47
+ "jsdoc/check-tag-names": "error",
48
+ "jsdoc/check-types": "error",
49
+ "jsdoc/implements-on-classes": "error",
50
+ "jsdoc/require-description": "error",
51
+ "jsdoc/require-jsdoc": [
52
+ "error",
53
+ {
54
+ require: {
55
+ FunctionDeclaration: true,
56
+ MethodDefinition: true,
57
+ ClassDeclaration: true,
58
+ ArrowFunctionExpression: false,
59
+ FunctionExpression: false,
60
+ },
61
+ },
62
+ ],
63
+ "jsdoc/require-param": "error",
64
+ "jsdoc/require-param-description": "error",
65
+ "jsdoc/require-param-type": "off",
66
+ "jsdoc/require-returns": "error",
67
+ "jsdoc/require-returns-description": "error",
68
+ "jsdoc/require-returns-type": "off",
69
+ "jsdoc/require-hyphen-before-param-description": ["error", "always"],
70
+ },
71
+ },
72
+ ];
package/index.ts ADDED
@@ -0,0 +1,88 @@
1
+ import { config } from "dotenv";
2
+ import { Hex } from "viem";
3
+ import { privateKeyToAccount } from "viem/accounts";
4
+ import { decodeXPaymentResponse, wrapFetchWithPayment } from "x402-fetch";
5
+
6
+ // Determine which env file to use
7
+ const args = process.argv.slice(2);
8
+ const envArg = args.find(arg => arg.startsWith("--env="));
9
+ const env = envArg ? envArg.split("=")[1] : "default";
10
+ console.log("env: " + env);
11
+
12
+ let envPath = ".env";
13
+ if (env !== "default") {
14
+ envPath = `.env-${env}`;
15
+ }
16
+ console.log("using env file: " + envPath);
17
+
18
+ // Set dotenv config to use correct .env file
19
+ config({ path: envPath });
20
+ console.log("using network: " + process.env.NETWORK);
21
+
22
+ // Set x402 constants from .env file
23
+ const privateKey = `0x${process.env.PRIVATE_KEY}` as Hex;
24
+ const explorerBaseUrl = process.env.EXPLORER_BASE_URL as string;
25
+ const account = privateKeyToAccount(privateKey);
26
+
27
+ // Set API url to query from .env file
28
+ const baseURL = process.env.API_SERVER_URL as string;
29
+ // Parse endpoint flag (if any)
30
+ const endpointArg = args.find(arg => arg.startsWith("--endpoint="));
31
+ const endpoint = endpointArg ? endpointArg.split("=")[1] : "metadata";
32
+ // Pick the right endpoint path based on flag
33
+ const endpointPath = endpoint === "check"
34
+ ? process.env.CHECK_ROBOTS_API_PATH as string
35
+ : process.env.METADATA_API_PATH as string;
36
+ // Rest of the querystring params
37
+ const metadataUrl = process.env.METADATA_URL as string; // url to fetch metadata from
38
+ const boolInclRespBody = process.env.METADATA_API_PARAM_INCLUDE_RESPONSE_BODY as string;
39
+ const paramInclRespBody = boolInclRespBody === 'true' ? `&includeResponseBody=true` : '';
40
+
41
+ if (endpoint === "check") {
42
+ if (!baseURL || !endpointPath || !metadataUrl) {
43
+ console.error("Missing required environment variables");
44
+ process.exit(1);
45
+ }
46
+ const url = `${baseURL}${endpointPath}?url=${metadataUrl}`;
47
+ console.log("querying url via checkURL: " + url);
48
+ const checkURL = fetch(url, { method: "GET"})
49
+ .then(async response => {
50
+ const body = await response.json();
51
+ console.log("response body:");
52
+ console.log(body);
53
+ })
54
+ .catch(error => {
55
+ console.log("ERROR!");
56
+ console.dir(error);
57
+ console.error(error.response?.data?.error);
58
+ });
59
+
60
+ } else {
61
+ if (!privateKey || !baseURL || !endpointPath || !metadataUrl) {
62
+ console.error("Missing required environment variables");
63
+ process.exit(1);
64
+ }
65
+ const url = `${baseURL}${endpointPath}?url=${metadataUrl}${paramInclRespBody}`;
66
+ console.log("querying url via fetchWithPayment: " + url);
67
+
68
+ // Fetch metadata url endpoint with payment
69
+ const fetchWithPayment = wrapFetchWithPayment(fetch, account);
70
+ fetchWithPayment(url, {
71
+ method: "GET",
72
+ })
73
+ .then(async response => {
74
+ const body = await response.json();
75
+ console.log("response body:");
76
+ console.log(body);
77
+
78
+ const paymentResponse = decodeXPaymentResponse(response.headers.get("x-payment-response")!);
79
+ console.log("payment response:");
80
+ console.log(paymentResponse);
81
+ console.log(`View transaction: ${explorerBaseUrl}/tx/${paymentResponse.transaction}`);
82
+ })
83
+ .catch(error => {
84
+ console.log("ERROR!");
85
+ console.dir(error);
86
+ console.error(error.response?.data?.error);
87
+ });
88
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "minifetch-api",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "bugs": {
6
+ "url": "https://github.com/Niche-Networks/minifetch-api/issues"
7
+ },
8
+ "homepage": "https://minifetch.com/ ",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git@github.com:Niche-Networks/minifetch-api.git"
12
+ },
13
+ "scripts": {
14
+ "dev:testnet": "tsx index.ts --env=base-testnet",
15
+ "dev:testnet:check": "tsx index.ts --env=base-testnet --endpoint=check",
16
+ "format": "prettier -c .prettierrc --write \"**/*.{ts,js,cjs,json,md}\"",
17
+ "format:check": "prettier -c .prettierrc --check \"**/*.{ts,js,cjs,json,md}\"",
18
+ "lint": "eslint . --ext .ts --fix",
19
+ "lint:check": "eslint . --ext .ts"
20
+ },
21
+ "dependencies": {
22
+ "dotenv": "^16.4.7",
23
+ "viem": "^2.23.1",
24
+ "x402-fetch": "0.6.0"
25
+ },
26
+ "devDependencies": {
27
+ "@eslint/js": "^9.24.0",
28
+ "eslint": "^9.24.0",
29
+ "eslint-plugin-jsdoc": "^50.6.9",
30
+ "eslint-plugin-prettier": "^5.2.6",
31
+ "@typescript-eslint/eslint-plugin": "^8.29.1",
32
+ "@typescript-eslint/parser": "^8.29.1",
33
+ "eslint-plugin-import": "^2.31.0",
34
+ "prettier": "3.5.2",
35
+ "tsx": "^4.7.0",
36
+ "typescript": "^5.3.0"
37
+ }
38
+ }