### Installing CogentJS with Package Managers Source: https://github.com/abr4xas/cogent-js/blob/develop/README.md Provides instructions for installing the CogentJS package using different Node.js package managers: pnpm, npm, and yarn. This is essential for integrating the library into a project. ```bash pnpm add @abr4xas/cogent-js ``` ```bash npm install @abr4xas/cogent-js ``` ```bash yarn add @abr4xas/cogent-js ``` -------------------------------- ### GitHub Package Registry Configuration Source: https://github.com/abr4xas/cogent-js/blob/develop/README.md Explains how to configure your npm client to use the GitHub Package Registry for installing the CogentJS package. This involves setting the registry URL in `.npmrc` or authenticating with a token. ```bash @abr4xas:registry=https://npm.pkg.github.com ``` ```bash echo "//npm.pkg.github.com/:_authToken=YOUR_GITHUB_TOKEN" >> ~/.npmrc ``` -------------------------------- ### Integrate CogentJS with Fetch API for HTTP Requests Source: https://context7.com/abr4xas/cogent-js/llms.txt Utilize CogentJS to generate dynamic URLs and then use the native Fetch API to make HTTP requests. This example shows how to create asynchronous functions to fetch data with filters, including pagination and authorization headers. Error handling for fetch requests is also demonstrated. ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query({ base_url: 'https://api.example.com' }); // Async function to fetch users with filters async function fetchUsers() { const url = query .for('users') .where('status', 'active') .includes('profile', 'posts') .select('id', 'name', 'email') .sort('-created_at') .limit(50) .page(1) .get(); try { const response = await fetch(url, { method: 'GET', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_TOKEN_HERE' } }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); console.log('Users:', data); return data; } catch (error) { console.error('Error fetching users:', error); throw error; } } // Async function to fetch paginated posts async function fetchPosts(pageNumber = 1) { const url = query .for('posts') .where('published', 'true') .whereIn('category', ['tech', 'news']) .includes('author', 'comments') .appends('read_time') .sort('-published_at') .limit(20) .page(pageNumber) .get(); try { const response = await fetch(url); const data = await response.json(); return { posts: data.data, meta: data.meta, nextPage: data.meta.current_page < data.meta.last_page ? pageNumber + 1 : null }; } catch (error) { console.error('Error fetching posts:', error); throw error; } } // Usage fetchUsers().then(users => console.log('Active users:', users)); fetchPosts(1).then(result => console.log('Posts page 1:', result)); ``` -------------------------------- ### Build Complex API Queries with CogentJS Source: https://context7.com/abr4xas/cogent-js/llms.txt Construct comprehensive API queries by combining all available methods for advanced API requests. This example demonstrates chaining methods like 'for', 'where', 'whereIn', 'includes', 'appends', 'select', 'sort', 'limit', 'page', and 'params' to generate a complex URL. The Query object can be reused for different API endpoints. ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query({ base_url: 'https://api.example.com' }); // Complex query with all features const complexQuery = query .for('posts') .where('status', 'published') .whereIn('category', ['tech', 'programming', 'javascript']) .includes('author', 'comments.user', 'tags') .appends('reading_time', 'view_count') .select({ posts: ['id', 'title', 'content', 'published_at'], author: ['id', 'name', 'avatar'], comments: ['id', 'body', 'created_at'] }) .sort('-published_at', 'title') .limit(20) .page(1) .params({ include_draft_comments: 'false', api_version: 'v1' }) .get(); // Returns: https://api.example.com/posts?include=author,comments.user,tags&append=reading_time,view_count&fields[posts][posts]=id,title,content,published_at&fields[posts][author]=id,name,avatar&fields[posts][comments]=id,body,created_at&filter[status]=published&filter[category]=tech,programming,javascript&sort=-published_at,title&page=1&limit=20&include_draft_comments=false&api_version=v1 // Reusable query object const url1 = query .for('users') .where('role', 'admin') .get(); // Returns: https://api.example.com/users?filter[role]=admin const url2 = query .for('products') .where('in_stock', 'true') .sort('-price') .get(); // Returns: https://api.example.com/products?filter[in_stock]=true&sort=-price ``` -------------------------------- ### Basic URL Construction with CogentJS Source: https://github.com/abr4xas/cogent-js/blob/develop/README.md Demonstrates the fundamental usage of CogentJS to build a URL for fetching posts with filtering, inclusion, and sorting. It shows how to import the Query class and chain methods to construct the desired API endpoint. ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); // /posts?filter[name]=Bob&include=posts,comments&sort=-created_at const url = query .for('posts') // the model you're selecting .where('name', 'Bob') // where the models `name` is 'Bob' .includes('posts', 'comments') // include the models related relationships: posts and comments .sort('-created_at') // sort by -created_at desc .get(); // generate the url and pass it into fetch! ``` -------------------------------- ### Pagination with limit() and page() in CogentJS Source: https://github.com/abr4xas/cogent-js/blob/develop/README.md Explains how to implement pagination by combining the `limit()` method with the `page()` method to specify the desired page number and the number of items per page. ```javascript // /users?page=2&limit=5 const url = query.for('users').limit(5).page(2).url(); // or .get(); ``` -------------------------------- ### Setting Base URL with CogentJS Source: https://github.com/abr4xas/cogent-js/blob/develop/README.md Illustrates how to configure a default base URL for all generated URLs when instantiating the CogentJS Query class. This simplifies URL construction when interacting with a specific API endpoint. ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query({ base_url: 'http://api.example.com' }); // http://api.example.com/users?filter[name]=Bob const url = query.for('users').where('name', 'Bob').url(); // or .get(); ``` -------------------------------- ### Pagination with page() and limit() Source: https://context7.com/abr4xas/cogent-js/llms.txt Implement pagination by specifying page number and results per page using limit() and page() methods. ```APIDOC ## GET /posts ### Description Retrieves posts with a limit of 10 results per page. ### Method GET ### Endpoint /posts?limit=10 ### Query Parameters - **limit** (integer) - Optional - The maximum number of results to return per page. ### Request Example ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); const limitOnly = query.for('posts').limit(10).get(); ``` ### Response #### Success Response (200) - **data** (array) - Array of post objects, up to the specified limit. #### Response Example ```json { "data": [ { "id": 1, "title": "Post 1" }, { "id": 2, "title": "Post 2" }, // ... up to 10 posts ] } ``` ``` ```APIDOC ## GET /products ### Description Retrieves products for a specific category, paginated with 25 results per page starting from page 2. ### Method GET ### Endpoint /products?filter[category]=electronics&page=2&limit=25 ### Query Parameters - **filter[category]** (string) - Optional - Filters results by category. - **page** (integer) - Optional - The page number to retrieve. - **limit** (integer) - Optional - The maximum number of results to return per page. ### Request Example ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); const paginated = query.for('products').limit(25).page(2).where('category', 'electronics').get(); ``` ### Response #### Success Response (200) - **data** (array) - Array of product objects for the specified page and limit. #### Response Example ```json { "data": [ { "id": 26, "name": "Product 26" }, // ... up to 25 products from page 2 ] } ``` ``` ```APIDOC ## GET /users ### Description Loads active users, sorted by creation date, with pagination set to page 3 and 50 results per page, selecting specific fields. ### Method GET ### Endpoint /users?fields[users]=id,name,email&filter[status]=active&sort=-created_at&page=3&limit=50 ### Query Parameters - **fields[users]** (string) - Optional - Specifies fields for the 'users' resource. - **filter[status]** (string) - Optional - Filters results by status. - **sort** (string) - Optional - Field name to sort by. - **limit** (integer) - Optional - The maximum number of results to return per page. - **page** (integer) - Optional - The page number to retrieve. ### Request Example ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); const fullPagination = query.for('users').where('status', 'active').sort('-created_at').limit(50).page(3).select('id', 'name', 'email').get(); ``` ### Response #### Success Response (200) - **data** (array) - Array of user objects for the specified page, limit, and filters. #### Response Example ```json { "data": [ { "id": 101, "name": "User 101", "email": "user101@example.com" }, // ... up to 50 users from page 3 ] } ``` ``` -------------------------------- ### Selecting Specific Fields with select() in CogentJS Source: https://github.com/abr4xas/cogent-js/blob/develop/README.md Explains how to use the `select()` method to specify which fields of a resource should be returned in the API response. This helps in optimizing data transfer. ```javascript // /users?fields=name,age,date_of_birth const url = query.for('users').select('name', 'age', 'date_of_birth').url(); // or .get(); ``` -------------------------------- ### Build Basic REST API URLs with CogentJS Source: https://context7.com/abr4xas/cogent-js/llms.txt This snippet demonstrates how to initialize CogentJS and construct basic API URLs for filtering resources. It shows how to chain methods like `for()` and `where()` to specify the resource and apply filters. ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); // Basic filtering const url = query .for('users') .where('name', 'Bob') .get(); // Returns: /users?filter[name]=Bob // Multiple filters const complexUrl = query .for('posts') .where('status', 'published') .where('author_id', 123) .get(); // Returns: /posts?filter[status]=published&filter[author_id]=123 ``` -------------------------------- ### Implement Pagination with page() and limit() in JavaScript Source: https://context7.com/abr4xas/cogent-js/llms.txt Pagination is implemented using the page() and limit() methods to control the number of results per page and the specific page requested. These methods are essential for handling large datasets efficiently. Dependencies include the '@abr4xas/cogent-js' library. ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); // Set result limit only const limitOnly = query .for('posts') .limit(10) .get(); // Returns: /posts?limit=10 // Paginate with specific page and limit const paginated = query .for('products') .limit(25) .page(2) .where('category', 'electronics') .get(); // Returns: /products?filter[category]=electronics&page=2&limit=25 // Complete pagination example with sorting const fullPagination = query .for('users') .where('status', 'active') .sort('-created_at') .limit(50) .page(3) .select('id', 'name', 'email') .get(); // Returns: /users?fields[users]=id,name,email&filter[status]=active&sort=-created_at&page=3&limit=50 ``` -------------------------------- ### Adding Custom Parameters with params() in CogentJS Source: https://github.com/abr4xas/cogent-js/blob/develop/README.md Shows how to append arbitrary custom query parameters to the URL using the `params()` method. This is useful for API-specific parameters not covered by the standard methods. ```javascript // /users?format=admin const url = query.for('users').params({ format: 'admin' }).url(); // or .get(); ``` -------------------------------- ### Filtering with where() in CogentJS Source: https://github.com/abr4xas/cogent-js/blob/develop/README.md Shows how to use the `where()` method in CogentJS to add a simple equality filter to the URL. This is useful for specifying conditions on resource properties. ```javascript // /users?filter[name]=Bob const url = query.for('users').where('name', 'Bob').url(); // or .get(); ``` -------------------------------- ### Sorting Results with sort() in CogentJS Source: https://github.com/abr4xas/cogent-js/blob/develop/README.md Illustrates how to use the `sort()` method to specify the order in which resources should be returned. It supports both ascending and descending sorts. ```javascript // /users?sort=-name,age const url = query.for('users').sort('-name', 'age').url(); // or .get(); ``` -------------------------------- ### Limiting Results with limit() in CogentJS Source: https://github.com/abr4xas/cogent-js/blob/develop/README.md Demonstrates the `limit()` method for controlling the maximum number of resources returned in a request. This is a standard pagination or result set sizing parameter. ```javascript // /users?limit=5 const url = query.for('users').limit(5).url(); // or .get(); ``` -------------------------------- ### Configure CogentJS with Base URL Source: https://context7.com/abr4xas/cogent-js/llms.txt This snippet illustrates how to configure the CogentJS Query instance with a base URL. This base URL is automatically prepended to all generated URLs, simplifying requests to a specific API endpoint. ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query({ base_url: 'https://api.example.com' }); const url = query .for('users') .where('active', 'true') .includes('profile') .get(); // Returns: https://api.example.com/users?include=profile&filter[active]=true // The query object can be reused for multiple requests const anotherUrl = query .for('posts') .where('category', 'tech') .get(); // Returns: https://api.example.com/posts?filter[category]=tech ``` -------------------------------- ### Custom Parameters with params() Source: https://context7.com/abr4xas/cogent-js/llms.txt Add arbitrary custom query parameters beyond the standard filtering, sorting, and pagination options. ```APIDOC ## GET /reports ### Description Retrieves reports with a custom 'format' parameter set to 'pdf'. ### Method GET ### Endpoint /reports?format=pdf ### Query Parameters - **format** (string) - Custom parameter - Specifies the desired output format. ### Request Example ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); const singleParam = query.for('reports').params({ format: 'pdf' }).get(); ``` ### Response #### Success Response (200) - **data** (any) - The report data in the requested format. #### Response Example ```json { "data": "PDF report content..." } ``` ``` ```APIDOC ## GET /data ### Description Retrieves data with multiple custom parameters, including format, version, debug flag, and locale, filtered by type. ### Method GET ### Endpoint /data?filter[type]=analytics&format=json&version=v2&debug=true&locale=en_US ### Query Parameters - **filter[type]** (string) - Optional - Filters results by type. - **format** (string) - Custom parameter - Specifies the data format. - **version** (string) - Custom parameter - Specifies the API version. - **debug** (string) - Custom parameter - Enables debug mode. - **locale** (string) - Custom parameter - Specifies the locale. ### Request Example ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); const multiParams = query.for('data').params({ format: 'json', version: 'v2', debug: 'true', locale: 'en_US' }).where('type', 'analytics').get(); ``` ### Response #### Success Response (200) - **data** (object) - The requested data with applied filters and custom parameters. #### Response Example ```json { "data": { "analytics_data": [...], "metadata": {"version": "v2", "locale": "en_US"} } } ``` ``` -------------------------------- ### Including Related Resources with includes() in CogentJS Source: https://github.com/abr4xas/cogent-js/blob/develop/README.md Illustrates the `includes()` method, which is used to request related resources or relationships to be fetched along with the primary resource. This reduces the number of separate API requests. ```javascript // /users?include=posts const url = query.for('users').includes('posts').url(); // or .get(); ``` -------------------------------- ### Including Relationships with includes() Source: https://context7.com/abr4xas/cogent-js/llms.txt Load related resources in a single request using the includes() method for eager loading. ```APIDOC ## GET /users ### Description Loads users and their related posts. ### Method GET ### Endpoint /users?include=posts ### Query Parameters - **include** (string) - Optional - Specifies relationships to eager load. Can be a single relationship or a comma-separated list. Supports dot notation for nested relationships. ### Request Example ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); const singleRelation = query.for('users').includes('posts').get(); ``` ### Response #### Success Response (200) - **data** (array) - Array of user objects, potentially including nested related data. #### Response Example ```json { "data": [ { "id": 1, "name": "John Doe", "posts": [ { "id": 101, "title": "First Post" }, { "id": 102, "title": "Second Post" } ] } ] } ``` ``` ```APIDOC ## GET /posts ### Description Loads posts with their author, comments, and tags, filtered by status. ### Method GET ### Endpoint /posts?include=author,comments,tags&filter[status]=published ### Query Parameters - **include** (string) - Optional - Specifies relationships to eager load (e.g., 'author,comments,tags'). - **filter[status]** (string) - Optional - Filters results by the 'status' field. ### Request Example ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); const multipleRelations = query.for('posts').includes('author', 'comments', 'tags').where('status', 'published').get(); ``` ### Response #### Success Response (200) - **data** (array) - Array of post objects, including author, comments, and tags. #### Response Example ```json { "data": [ { "id": 101, "title": "Sample Post", "author": {"id": 1, "name": "John Doe"}, "comments": [{"id": 1, "body": "Great post!"}], "tags": [{"id": 1, "name": "API"}] } ] } ``` ``` ```APIDOC ## GET /users ### Description Loads users with their posts' comments and tags, and their profile, selecting specific user fields. ### Method GET ### Endpoint /users?include=posts.comments,posts.tags,profile&fields[users]=id,name,email ### Query Parameters - **include** (string) - Optional - Specifies nested relationships to eager load (e.g., 'posts.comments,posts.tags,profile'). - **fields[users]** (string) - Optional - Specifies the fields to return for the 'users' resource (e.g., 'id,name,email'). ### Request Example ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); const nestedRelations = query.for('users').includes('posts.comments', 'posts.tags', 'profile').select('id', 'name', 'email').get(); ``` ### Response #### Success Response (200) - **data** (array) - Array of user objects with specified fields and nested relationships. #### Response Example ```json { "data": [ { "id": 1, "name": "John Doe", "email": "john.doe@example.com", "posts": [ { "id": 101, "comments": [{"id": 1, "body": "Comment 1"}] } ], "profile": {"id": 1, "bio": "User profile bio"} } ] } ``` ``` -------------------------------- ### Customize Query Parameter Names in JavaScript Source: https://context7.com/abr4xas/cogent-js/llms.txt The Query constructor accepts an optional configuration object to customize the names of query parameters, allowing them to match specific API conventions. This provides flexibility in how requests are formed. Dependencies include the '@abr4xas/cogent-js' library. ```javascript import { Query } from '@abr4xas/cogent-js'; // Configure custom parameter names const query = new Query({ queryParameters: { filters: 'where', fields: 'select', includes: 'with', appends: 'add', page: 'p', limit: 'per_page', sort: 'order' } }); const customUrl = query .for('users') .where('status', 'active') .select('id', 'name') .includes('posts') .appends('full_name') .sort('-created_at') .page(1) .limit(20) .get(); // Returns: /users?with=posts&add=full_name&select[users]=id,name&where[status]=active&order=-created_at&p=1&per_page=20 ``` -------------------------------- ### Select Specific Fields with select() in CogentJS Source: https://context7.com/abr4xas/cogent-js/llms.txt This snippet demonstrates how to use the `select()` method in CogentJS to specify which fields to include in the API response. It covers selecting fields for a single resource, using array syntax, and selecting fields for multiple related resources. ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); // Select specific fields for a single resource const simpleSelect = query .for('users') .select('id', 'name', 'email') .get(); // Returns: /users?fields[users]=id,name,email // Select fields using array syntax const arraySelect = query .for('products') .select(['title', 'price', 'stock']) .get(); // Returns: /products?fields[products]=title,price,stock // Select fields for multiple related resources const relatedSelect = query .for('posts') .select({ posts: ['title', 'content', 'published_at'], authors: ['name', 'email', 'bio'] }) .includes('authors') .get(); // Returns: /posts?include=authors&fields[posts][posts]=title,content,published_at&fields[posts][authors]=name,email,bio ``` -------------------------------- ### Load Relationships with includes() in JavaScript Source: https://context7.com/abr4xas/cogent-js/llms.txt The includes() method allows for eager loading of related resources in a single API request. It supports including single, multiple, and nested relationships using dot notation. Dependencies include the '@abr4xas/cogent-js' library. ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); // Include single relationship const singleRelation = query .for('users') .includes('posts') .get(); // Returns: /users?include=posts // Include multiple relationships const multipleRelations = query .for('posts') .includes('author', 'comments', 'tags') .where('status', 'published') .get(); // Returns: /posts?include=author,comments,tags&filter[status]=published // Include nested relationships using dot notation const nestedRelations = query .for('users') .includes('posts.comments', 'posts.tags', 'profile') .select('id', 'name', 'email') .get(); // Returns: /users?include=posts.comments,posts.tags,profile&fields[users]=id,name,email ``` -------------------------------- ### Customizing Query Parameter Names in CogentJS Source: https://github.com/abr4xas/cogent-js/blob/develop/README.md Explains how to override the default names for query parameters (like 'include', 'filter', 'sort') by providing a custom configuration object during Query instantiation. This allows for compatibility with APIs that use different parameter naming conventions. ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query({ queryParameters: { include: 'include_custom', filters: 'filter_custom', sort: 'sort_custom', fields: 'fields_custom', append: 'append_custom', page: 'page_custom', limit: 'limit_custom' } }); ``` -------------------------------- ### Sorting with sort() Source: https://context7.com/abr4xas/cogent-js/llms.txt Define the sort order for results using field names with optional minus prefix for descending order. ```APIDOC ## GET /products ### Description Retrieves products sorted by price in ascending order. ### Method GET ### Endpoint /products?sort=price ### Query Parameters - **sort** (string) - Optional - Field name to sort by. Prepend with '-' for descending order. ### Request Example ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); const ascendingSort = query.for('products').sort('price').get(); ``` ### Response #### Success Response (200) - **data** (array) - Array of product objects sorted by price. #### Response Example ```json { "data": [ { "id": 1, "name": "Keyboard", "price": 75.00 }, { "id": 2, "name": "Mouse", "price": 25.00 }, { "id": 3, "name": "Monitor", "price": 300.00 } ] } ``` ``` ```APIDOC ## GET /posts ### Description Retrieves posts sorted by creation date in descending order. ### Method GET ### Endpoint /posts?sort=-created_at ### Query Parameters - **sort** (string) - Optional - Field name to sort by. Prepend with '-' for descending order. ### Request Example ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); const descendingSort = query.for('posts').sort('-created_at').get(); ``` ### Response #### Success Response (200) - **data** (array) - Array of post objects sorted by creation date descending. #### Response Example ```json { "data": [ { "id": 102, "title": "Latest Post", "created_at": "2023-10-27T10:00:00Z" }, { "id": 101, "title": "Older Post", "created_at": "2023-10-26T15:30:00Z" } ] } ``` ``` ```APIDOC ## GET /users ### Description Loads users sorted by creation date descending, then by name ascending, filtered by active status. ### Method GET ### Endpoint /users?filter[active]=true&sort=-created_at,name,email ### Query Parameters - **filter[active]** (string) - Optional - Filters results by the 'active' field. - **sort** (string) - Optional - Field names to sort by, comma-separated. Prepend with '-' for descending order. ### Request Example ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); const multiSort = query.for('users').sort('-created_at', 'name', 'email').where('active', 'true').get(); ``` ### Response #### Success Response (200) - **data** (array) - Array of user objects sorted by multiple criteria. #### Response Example ```json { "data": [ { "id": 1, "name": "Alice", "created_at": "2023-10-27T09:00:00Z" }, { "id": 2, "name": "Bob", "created_at": "2023-10-27T08:00:00Z" }, { "id": 3, "name": "Charlie", "created_at": "2023-10-26T18:00:00Z" } ] } ``` ``` -------------------------------- ### Custom Query Parameter Names Source: https://context7.com/abr4xas/cogent-js/llms.txt Customize the query parameter names to match your API's conventions or requirements. ```APIDOC ## GET /users ### Description Constructs a URL for fetching users with custom query parameter names for filters, selects, includes, appends, pagination, and sorting. ### Method GET ### Endpoint /users?with=posts&add=full_name&select[users]=id,name&where[status]=active&order=-created_at&p=1&per_page=20 ### Query Parameters - **with** (string) - Custom parameter name for 'include' - Specifies relationships to eager load. - **add** (string) - Custom parameter name for 'append' - Specifies computed attributes to append. - **select[users]** (string) - Custom parameter name for 'fields' - Specifies fields for the 'users' resource. - **where[status]** (string) - Custom parameter name for 'filter' - Filters results by status. - **order** (string) - Custom parameter name for 'sort' - Field name to sort by. - **p** (integer) - Custom parameter name for 'page' - The page number to retrieve. - **per_page** (integer) - Custom parameter name for 'limit' - The maximum number of results to return per page. ### Request Example ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query({ queryParameters: { filters: 'where', fields: 'select', includes: 'with', appends: 'add', page: 'p', limit: 'per_page', sort: 'order' } }); const customUrl = query .for('users') .where('status', 'active') .select('id', 'name') .includes('posts') .appends('full_name') .sort('-created_at') .page(1) .limit(20) .get(); ``` ### Response #### Success Response (200) - **data** (array) - Array of user objects matching the query parameters. #### Response Example ```json { "data": [ { "id": 1, "name": "Alice", "full_name": "Alice Smith" }, // ... users matching the criteria ] } ``` ``` -------------------------------- ### Add Custom Parameters with params() in JavaScript Source: https://context7.com/abr4xas/cogent-js/llms.txt The params() method allows for the inclusion of arbitrary custom query parameters in the API request, extending beyond standard filtering and sorting options. It accepts an object of key-value pairs. Dependencies include the '@abr4xas/cogent-js' library. ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); // Add single custom parameter const singleParam = query .for('reports') .params({ format: 'pdf' }) .get(); // Returns: /reports?format=pdf // Add multiple custom parameters const multiParams = query .for('data') .params({ format: 'json', version: 'v2', debug: 'true', locale: 'en_US' }) .where('type', 'analytics') .get(); // Returns: /data?filter[type]=analytics&format=json&version=v2&debug=true&locale=en_US ``` -------------------------------- ### Filtering with whereIn() in CogentJS Source: https://github.com/abr4xas/cogent-js/blob/develop/README.md Demonstrates the use of the `whereIn()` method for adding filters that match a value against a list of possibilities. This is commonly used for filtering by multiple IDs or values. ```javascript // /users?filter[name]=bob,jerry const url = query.for('users').whereIn('name', ['bob', 'jerry']).url(); // or .get(); ``` -------------------------------- ### Appending Additional Data with appends() in CogentJS Source: https://github.com/abr4xas/cogent-js/blob/develop/README.md Shows how to use the `appends()` method to request additional, often computed or virtual, attributes for a resource. These are typically appended to the main resource data. ```javascript // /users?append=full_name,age const url = query.for('users').appends('full_name', 'age').url(); // or .get(); ``` -------------------------------- ### Filter API Results with where() and whereIn() in CogentJS Source: https://context7.com/abr4xas/cogent-js/llms.txt This snippet shows how to use `where()` for single-value filters and `whereIn()` for multiple-value filters in CogentJS. It demonstrates building complex queries by combining various filtering conditions. ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); // Single value filter const singleFilter = query .for('products') .where('category', 'electronics') .where('in_stock', 'true') .get(); // Returns: /products?filter[category]=electronics&filter[in_stock]=true // Multiple value filter using whereIn const multipleValues = query .for('users') .whereIn('role', ['admin', 'editor', 'moderator']) .where('active', 'true') .get(); // Returns: /users?filter[role]=admin,editor,moderator&filter[active]=true // Combining filters for complex queries const complexFilters = query .for('orders') .where('status', 'completed') .whereIn('payment_method', ['credit_card', 'paypal']) .where('total_amount', '100') .get(); // Returns: /orders?filter[status]=completed&filter[payment_method]=credit_card,paypal&filter[total_amount]=100 ``` -------------------------------- ### Sort Results with sort() in JavaScript Source: https://context7.com/abr4xas/cogent-js/llms.txt The sort() method defines the order of results returned by the API. It accepts field names and can specify descending order using a minus prefix. Multiple fields can be sorted simultaneously. Dependencies include the '@abr4xas/cogent-js' library. ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); // Sort by single field ascending const ascendingSort = query .for('products') .sort('price') .get(); // Returns: /products?sort=price // Sort by single field descending (minus prefix) const descendingSort = query .for('posts') .sort('-created_at') .get(); // Returns: /posts?sort=-created_at // Sort by multiple fields with mixed directions const multiSort = query .for('users') .sort('-created_at', 'name', 'email') .where('active', 'true') .get(); // Returns: /users?filter[active]=true&sort=-created_at,name,email ``` -------------------------------- ### Appending Computed Attributes with appends() Source: https://context7.com/abr4xas/cogent-js/llms.txt Request computed or accessor attributes from the API that aren't normally included in responses. ```APIDOC ## GET /users ### Description Loads users, appending computed attributes like 'full_name' and 'age', filtered by active status. ### Method GET ### Endpoint /users?append=full_name,age,is_premium&filter[active]=true ### Query Parameters - **append** (string) - Optional - Specifies computed attributes to append to the response (e.g., 'full_name,age,is_premium'). - **filter[active]** (string) - Optional - Filters results by the 'active' field. ### Request Example ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); const withAppends = query.for('users').appends('full_name', 'age', 'is_premium').where('active', 'true').get(); ``` ### Response #### Success Response (200) - **data** (array) - Array of user objects, including the appended computed attributes. #### Response Example ```json { "data": [ { "id": 1, "first_name": "John", "last_name": "Doe", "full_name": "John Doe", "age": 30, "is_premium": false } ] } ``` ``` ```APIDOC ## GET /products ### Description Loads products with selected fields, appended computed attributes, and their category. ### Method GET ### Endpoint /products?include=category&append=formatted_price,discount_percentage&fields[products]=id,name,price ### Query Parameters - **include** (string) - Optional - Specifies relationships to eager load (e.g., 'category'). - **append** (string) - Optional - Specifies computed attributes to append (e.g., 'formatted_price,discount_percentage'). - **fields[products]** (string) - Optional - Specifies fields for the 'products' resource (e.g., 'id,name,price'). ### Request Example ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); const comprehensive = query.for('products').select('id', 'name', 'price').appends('formatted_price', 'discount_percentage').includes('category').get(); ``` ### Response #### Success Response (200) - **data** (array) - Array of product objects with selected fields, appended attributes, and included category. #### Response Example ```json { "data": [ { "id": 1, "name": "Laptop", "price": 1200.00, "formatted_price": "$1,200.00", "discount_percentage": 10, "category": {"id": 1, "name": "Electronics"} } ] } ``` ``` -------------------------------- ### Append Computed Attributes with appends() in JavaScript Source: https://context7.com/abr4xas/cogent-js/llms.txt The appends() method is used to request computed or accessor attributes from the API that are not typically part of the standard response. This is useful for including derived data. It can be combined with other methods like includes() and select(). Dependencies include the '@abr4xas/cogent-js' library. ```javascript import { Query } from '@abr4xas/cogent-js'; const query = new Query(); // Append computed attributes const withAppends = query .for('users') .appends('full_name', 'age', 'is_premium') .where('active', 'true') .get(); // Returns: /users?append=full_name,age,is_premium&filter[active]=true // Combine appends with includes and selects const comprehensive = query .for('products') .select('id', 'name', 'price') .appends('formatted_price', 'discount_percentage') .includes('category') .get(); // Returns: /products?include=category&append=formatted_price,discount_percentage&fields[products]=id,name,price ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.