{"id":18185050,"url":"https://github.com/jaandrle/fetch-test-api","last_synced_at":"2025-07-30T19:36:11.837Z","repository":{"id":222291031,"uuid":"756825279","full_name":"jaandrle/fetch-test-api","owner":"jaandrle","description":"“An alternative” to PostMan/Insomnia/…. Test REST API using script files (and `fetch`).","archived":false,"fork":false,"pushed_at":"2024-12-06T15:22:09.000Z","size":30,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-11T08:17:06.636Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jaandrle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-13T11:36:00.000Z","updated_at":"2024-12-06T15:22:12.000Z","dependencies_parsed_at":"2024-02-13T13:56:32.802Z","dependency_job_id":"1e72ddbe-4cc8-41a1-b00a-69c23a5a8c4c","html_url":"https://github.com/jaandrle/fetch-test-api","commit_stats":null,"previous_names":["jaandrle/fetch-test-api"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jaandrle/fetch-test-api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaandrle%2Ffetch-test-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaandrle%2Ffetch-test-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaandrle%2Ffetch-test-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaandrle%2Ffetch-test-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jaandrle","download_url":"https://codeload.github.com/jaandrle/fetch-test-api/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaandrle%2Ffetch-test-api/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259991223,"owners_count":22942587,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-02T22:05:49.660Z","updated_at":"2025-06-15T14:39:16.911Z","avatar_url":"https://github.com/jaandrle.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Test API using JavaScript (`fetch`)\nEffortlessly manage and execute API requests directly in your JavaScript codebase using\n`fetch`. This library is designed for modular and reusable API development, with centralized\nconfigurations and a developer-friendly structure. It serves as an alternative to [Huachao/vscode-restclient: REST Client Extension for Visual Studio Code](https://github.com/Huachao/vscode-restclient).\n\n## Features\n- Execute API requests directly through JavaScript.\n- Centralized configuration for URLs, headers, and environment variables (moreover if you already\n  use JavaScript in your project).\n- Modular and reusable structure for managing API endpoints.\n- Leverage plain JavaScript constants, variables, and functions.\n\n## Installation (TBD)\nTo start using the library, install the required dependency:\n```bash\n# TBD\n```\n\n## Usage\nWhile there is no strict pattern, a typical file structure for this library might look like this:\n\n```\napi/\n├── .api.js\n├── .config.js (or .config.json)\n├── .env.js (or .env, .env.json)\n├── .gitignore\n├── auth.js\n└── user/\n    ├── .config.js\n    ├── get.js\n    └── post.js\n```\n\n### File Conventions\n- **Executable Files**: Files not prefixed with a dot (e.g., `auth.js`) contain API logic and can\n  be executed directly.\n- **Configuration files** (`.config.js`, `.env.js`): store reusable settings. You may prefer\n  replacing them with already existing configuration/env files in your project. In case\n  of `.env` you may also need to install [dotenv](https://www.npmjs.com/package/dotenv).\n\n### `.gitignore` example\nYour `.gitignore` file might look like this:\n\n```\n.env.js\nresponse.js\nresponse-*.js\n```\n\n### Configuration Files\nBoth `.config.js` and `.env.js` can be used for configuration. Here are examples of what they might\ncontain:\n```JavaScript\n// .config.js\nexport const url = \"https://api.example.com/\";\nexport const headers= {\n\t\"content-type\": \"application/json\",\n\t\"accept\": \"application/json, plain/text, */*\"\n};\n// Additional configurations like version, etc.\n\n// .env.js\nexport const token = \"secret\";\n// Additional credentials like client_id, client_secret, etc.\n```\n\n### Common Utilities (`.api.js`)\nThe `.api.js` file consolidates configurations and provides utility imports for streamlined API\nrequests:\n```JavaScript\n// .api.js\nimport { readJSONFile, setPathRoot, fetch, fetchSave } from 'fetch-test-api';\nexport { readJSONFile, setPathRoot, fetch, fetchSave };\nsetPathRoot(import.meta.url);\n\nimport { url as base, headers } from \"./.config.mjs\";\nconst { version= \"v1\" }= process.env;\nconst url= base+version+\"/\";\nexport { url, headers };\n```\n\nThis structure simplifies imports in your executable files by centralizing configurations.\n\nA specific configuration (let’s say `version`) can also be defined in the `.config.js` file,\nor defined depending on the environment variable (`export const version= process.env.DEV ? \"dev\" :\n\"v1\"`).\n\n\n### `auth.js` example\nNow lets take a look at the `auth.js` file. This is first executable file, in wich we define the\nlogin logic:\n1. **Direct Execution**: Handles login logic and saves the response locally.\n1. **Import Usage**: Uses stored credentials to set authorization headers.\n\n```JavaScript\n#!/bin/env node\nimport { argv, exit } from \"node:process\";\nimport { pathToFileURL } from \"node:url\";\nimport { fetch, fetchSave, url, headers, readJSONFile } from \"./api.mjs\";\nconst file_login= \"./response-login.json\";\n\n// 1. Login Logic (Direct Execution)\nif(import.meta.url === pathToFileURL(argv[1]).href){\n\tconst { client_id, client_secret }= readJSONFile(\"./.env.json\");\n\tawait fetch(url+\"auth/token\", {\n\t\tmethod: \"POST\",\n\t\theaders,\n\t\tbody: JSON.stringify({\n\t\t\tclient_id,\n\t\t\tclient_secret,\n\t\t})\n\t})\n\t.then(fetchSave({\n\t\tpath: file_login\n\t}));\n\texit(0);\n}\n\n// 2. Use Credentials (Import Usage)\nconst { data }= readJSONFile(file_login, import.meta.url);\nheaders[\"Authorization\"]= `Bearer ${data.access_token}`;\n```\n\n### Recommended Practices\n1. **Organize Your Code**: Group related endpoints under directories (e.g. `user/get.js`, `user/post.js`).\n1. **Centralize Configuration**: Use `.api.js` to avoid repetitive imports and ensure consistent configurations.\n1. **Secure Sensitive Data**: Keep sensitive files (e.g. `.env.js`) out of version control with `.gitignore`.\n\n\n### `user/*` inspiration\n```JavaScript\n// user/.config.js\nexport const endpoint= \"user\";\n```\n\n```JavaScript\n// user/get.js\nimport { fetch, fetchSave, url, headers } from \"../.api.js\";\nimport { endpoint } from \"./.config.js\";\nimport \"../auth.js\";\n\nawait fetch(url+endpoint, { headers })\n\t.then(fetchSave());\n```\n\n```JavaScript\n// user/post.js\nimport { fetch, fetchSave, url, headers } from \"../.api.js\";\nimport { endpoint } from \"./.config.js\";\nimport \"../auth.js\";\n\nawait fetch(url+endpoint, {\n\tmethod: \"POST\",\n\theaders,\n\tbody: JSON.stringify({ name: \"John Doe\" })\n})\n\t.then(fetchSave());\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaandrle%2Ffetch-test-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjaandrle%2Ffetch-test-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaandrle%2Ffetch-test-api/lists"}