{"id":27498328,"url":"https://github.com/fabianlopezdev/fetch-wordpress-api","last_synced_at":"2026-04-10T21:03:32.466Z","repository":{"id":181711253,"uuid":"667187633","full_name":"fabianlopezdev/fetch-wordpress-api","owner":"fabianlopezdev","description":"Easy fetching from the Wordpress API","archived":false,"fork":false,"pushed_at":"2023-10-20T19:35:27.000Z","size":78,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-16T02:01:38.648Z","etag":null,"topics":["headless","headless-wodpress","javascript","rest","rest-api","typescript","wordpress","wordpress-api","wp"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/fabianlopezdev.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":"2023-07-16T23:34:51.000Z","updated_at":"2024-04-18T09:42:38.000Z","dependencies_parsed_at":"2024-09-08T22:03:20.007Z","dependency_job_id":"494c7325-f49e-4125-a904-3e9df8ab97e5","html_url":"https://github.com/fabianlopezdev/fetch-wordpress-api","commit_stats":{"total_commits":85,"total_committers":1,"mean_commits":85.0,"dds":0.0,"last_synced_commit":"aa863531ec5317e97817bbf1595b2a59384a3152"},"previous_names":["fabs-and/fetch-wordpress-api","fabianlopezdev/fetch-wordpress-api"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabianlopezdev%2Ffetch-wordpress-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabianlopezdev%2Ffetch-wordpress-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabianlopezdev%2Ffetch-wordpress-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabianlopezdev%2Ffetch-wordpress-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fabianlopezdev","download_url":"https://codeload.github.com/fabianlopezdev/fetch-wordpress-api/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249060973,"owners_count":21206432,"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":["headless","headless-wodpress","javascript","rest","rest-api","typescript","wordpress","wordpress-api","wp"],"created_at":"2025-04-17T08:32:22.228Z","updated_at":"2026-04-10T21:03:32.373Z","avatar_url":"https://github.com/fabianlopezdev.png","language":"TypeScript","readme":"# Fetch WordPress API\n\n[![npm version](https://badge.fury.io/js/fetch-wordpress-api.svg)](https://badge.fury.io/js/fetch-wordpress-api)\n![WordPress](https://img.shields.io/badge/WordPress-%23117AC9.svg?style=flat\u0026logo=WordPress\u0026logoColor=white)\n![TypeScript](https://img.shields.io/badge/typescript-%23007ACC.svg?style=flat\u0026logo=typescript\u0026logoColor=white)\n![JavaScript](https://img.shields.io/badge/javascript-%23323330.svg?style=flat\u0026logo=javascript\u0026logoColor=%23F7DF1E)\n[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=Fabs-and_fetch-wordpress-api\u0026metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=Fabs-and_fetch-wordpress-api)\n[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=Fabs-and_fetch-wordpress-api\u0026metric=security_rating)](https://sonarcloud.io/summary/new_code?id=Fabs-and_fetch-wordpress-api)\n[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=Fabs-and_fetch-wordpress-api\u0026metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=Fabs-and_fetch-wordpress-api)\n\nThis module provides a set of utility functions for interacting with a headless WordPress CMS via the WordPress REST API. It includes functions for fetching posts, pages, and categories, with support for custom fields and query parameters.\n\nInstallation\n\nTo install the package, run the following command:\n\n```\nnpm install fetch-wordpress-api\n```\n\n## Usage\n\nBefore using the package's functions, you need to configure the `BASE_URL` variable by calling the `configure` function and passing an object with the `BASE_URL` property set to the base URL of your WordPress site.\n\nHere's an example of how to configure the `BASE_URL` and use the package's functions:\n\n```typescript\nimport { configure, fetchPosts, PostFields } from 'fetch-wordpress-api';\n\nconfigure({ BASE_URL: 'https://example.com' });\n\n// This will return an array of posts objects\nconst posts = await fetchPosts();\n\n// Fetch only 5 posts\nconst posts = await fetchPosts(5);\n\n// Fetch 3 posts containing only the title, content, and categories\nconst postFields: PostFields[] = ['title', 'content', 'categories']; // Remember to import the type PostFields to get suggestions about the post fields available and to avoid passing anything other than a possible field.\n\nconst posts = await fetchPosts(3, postFields);\n```\n\nEach function in this module performs a fetch request to a specific WordPress API endpoint. They return a Promise that resolves with the fetched data as a JSON object or rejects with an Error if the fetch request fails.\n\nPlease refer to the source code and TypeScript type definitions for detailed information on the available functions and their parameters.\n\n## API\n\nThe package includes the following utility functions:\n\n- **`configure(options)`:**\n\nThis function sets up the package by using the provided options. It's crucial to use this function first in order to establish a connection with your Wordpress domain.\n\n`configure({BASE_URL: 'your-wordpress-domain'}).`\n\n- **`fetchData(endpoint?, query?)`:**\n\nMain function that all other utility functions use to retrieve data.\n\n- **`fetchPosts(quantity?, postFields?)`:**\n\nRetrieve either all posts or a specified number of posts. You can specify the fields you want returned for each post.\n\n`fetchPosts(5, ['id', 'title']).`\n\n**Note:** `fetchPosts()` with no arguments will retrieve all posts with all fields. If you still want to retrieve all posts, but just with certain fields (as opposed to all of them), you can pass -1 in quantity: `fetchPosts(-1, ['id', 'title'])`\n\n- **`fetchPostsInCategory(categoryId, postFields?, quantity?)`:**\n\nRetrieve posts from a specific category. You can specify the fields you want returned for each post and limit the number of posts.\n\n`fetchPostsInCategory(1, ['id', 'title'], 5).`\n\n- **`fetchPostBySlug(slug, postFields?)`:**\n\nRetrieve a post using its slug. You can specify the fields you want returned.\n\n`fetchPostBySlug('your/post-slug', ['id', 'title']).`\n\n- **`fetchPostById('id', postFields?)`:**\n\nRetrieve a post by its ID. You can specify the fields you want returned.\n\n`fetchPostById(123, ['id', 'title']).`\n\n- **`fetchAllCategories(categoryFields?)`:**\n\nRetrieve all categories. You can specify the fields you want for each category.\n\n`fetchAllCategories(['id', 'name']).`\n\n- **`fetchPages(quantity?, pageFields?)`**:\n\nRetrieve a specified number of pages. You can specify the fields you want returned for each page.\n\n`fetchPages(5, ['id', 'title']).`\n\n**Note:** `fetchPages()` with no arguments will retrieve all pages with all fields. If you still want to retrieve all pages, but just with certain fields (as opposed to all of them), you can pass -1 in quantity: `fetchPages(-1, ['id', 'title'])`\n\n- **`fetchPageBySlug(slug, pageFields?)`:**\n\nRetrieve a page using its slug. You can specify the fields you want returned.\n\n`fetchPageBySlug('page-slug', ['id', 'title']).`\n\n- **`fetchPageById(id, pageFields?)`:**\n\nRetrieve a page by its ID. You can specify the fields you want returned.\n\n`fetchPageById(123, ['id', 'title']).`\n\nFor more detailed information on the available functions and their parameters, please refer to the source code and TypeScript type definitions.\n\n## Importing Types\n\nIn addition to importing the functions, you can also import the Typescript types from this package. This can be useful when working with the functions and their return values in a Typescript project.\n\nTo import the types, simply include them in your import statement:\n\n```typescript\nimport type { CategoryFields } from 'fetch-wordpress-api';\n```\n\nBy importing the types, you can benefit from TypeScript's type checking and autocompletion features when using this package.\n\nThese are the types available to use in your application:\n\n```typescript\ntype Post = {\n  author: number;\n  categories: number[];\n  comment_status: string;\n  content: { rendered: string };\n  date: string;\n  date_gmt: string | null | Date;\n  excerpt: { rendered: string };\n  featured_media: number;\n  format: string;\n  guid: string;\n  id: { rendered: string; raw: string };\n  link: string | Url;\n  meta: Record\u003cstring, string | number | boolean | any[] | Record\u003cstring, any\u003e\u003e;\n  modified: string | Date;\n  modified_gmt: string | Date;\n  ping_status: string;\n  slug: string;\n  status: string;\n  sticky: string;\n  tags: number[];\n  template: string;\n  title: { rendered: string };\n  type: string;\n};\n\nexport type PostFields =\n  | 'author'\n  | 'categories'\n  | 'comment_status'\n  | 'content'\n  | 'date'\n  | 'date_gmt'\n  | 'excerpt'\n  | 'featured_media'\n  | 'format'\n  | 'guid'\n  | 'id'\n  | 'link'\n  | 'meta'\n  | 'modified'\n  | 'modified_gmt'\n  | 'ping_status'\n  | 'slug'\n  | 'status'\n  | 'sticky'\n  | 'tags'\n  | 'template'\n  | 'title'\n  | 'type';\n\ntype Category = {\n  count: number;\n  description: string;\n  id: number;\n  link: string;\n  meta: Record\u003cstring, string | number | boolean | any[] | Record\u003cstring, any\u003e\u003e;\n  name: string;\n  slug: string;\n  taxonomy: string;\n};\n\nexport type CategoryFields =\n  | 'count'\n  | 'description'\n  | 'id'\n  | 'link'\n  | 'meta'\n  | 'parent'\n  | 'name'\n  | 'slug'\n  | 'taxonomy';\n\nexport type PageFields =\n  | 'author'\n  | 'comment_status'\n  | 'content'\n  | 'date'\n  | 'date_gmt'\n  | 'excerpt'\n  | 'featured_media'\n  | 'generated_slug'\n  | 'guid'\n  | 'id'\n  | 'link'\n  | 'menu_order'\n  | 'meta'\n  | 'modified'\n  | 'modified_gmt'\n  | 'password'\n  | 'permalink_template'\n  | 'ping_status'\n  | 'slug'\n  | 'status'\n  | 'template'\n  | 'title'\n  | 'type';\n\ntype Page = {\n  author: number;\n  comment_status: 'open' | 'closed';\n  content: {\n    rendered: string;\n    raw?: string;\n    protected?: boolean;\n  };\n  date: string | null;\n  date_gmt: string | null;\n  excerpt: {\n    rendered: string;\n    raw?: string;\n    protected?: boolean;\n  };\n  featured_media: number;\n  generated_slug: string;\n  guid: {\n    rendered: string;\n  };\n  id: number;\n  link: string;\n  menu_order: number;\n  meta: Record\u003cstring, any\u003e;\n  modified: string;\n  modified_gmt: string;\n  parent: number;\n  password: string;\n  permalink_template: string;\n  ping_status: 'open' | 'closed';\n  slug: string;\n  status: 'publish' | 'future' | 'draft' | 'pending' | 'private';\n  template: string;\n  title: {\n    rendered: string;\n    raw?: string;\n  };\n  type: 'page';\n};\n```\n\n## Contributing\n\nIf you'd like to contribute to this project, please feel free to submit a pull request or open an issue on the GitHub repository.\n\n## License\n\nThis package is released under the MIT License.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabianlopezdev%2Ffetch-wordpress-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffabianlopezdev%2Ffetch-wordpress-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabianlopezdev%2Ffetch-wordpress-api/lists"}