https://github.com/jesusgraterol/api-response-utils
The api-response-utils package streamlines RESTful API data exchange by introducing a standardized data structure for HTTP response bodies. This structure simplifies client-side data handling, promoting consistency and readability in your API interactions.
https://github.com/jesusgraterol/api-response-utils
api api-response api-rest api-restful body http http-body rest rest-api restful restful-api utilities utils
Last synced: 5 months ago
JSON representation
The api-response-utils package streamlines RESTful API data exchange by introducing a standardized data structure for HTTP response bodies. This structure simplifies client-side data handling, promoting consistency and readability in your API interactions.
- Host: GitHub
- URL: https://github.com/jesusgraterol/api-response-utils
- Owner: jesusgraterol
- License: mit
- Created: 2024-05-31T18:21:35.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-03T15:38:03.000Z (about 1 year ago)
- Last Synced: 2025-01-04T13:03:23.740Z (about 1 year ago)
- Topics: api, api-response, api-rest, api-restful, body, http, http-body, rest, rest-api, restful, restful-api, utilities, utils
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/api-response-utils
- Size: 73.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# API Response Utils
The `api-response-utils` package streamlines RESTful API data exchange by introducing a standardized data structure for HTTP response bodies. This structure simplifies client-side data handling, promoting consistency and readability in your API interactions.
## Getting Started
Install the package:
```bash
npm i -S api-response-utils
```
### Examples
Building a successful response:
```typescript
import { buildResponse } from 'api-response-utils';
buildResponse();
// {
// success: true,
// data: undefined,
// error: undefined
// }
// building a successful response w/ data:
buildResponse({ id: 1, nickname: 'Jane Doe' });
// {
// success: true,
// data: { id: 1, nickname: 'Jane Doe' },
// error: undefined
// }
```
Building an unsuccessful response:
```typescript
import { buildResponse } from 'api-response-utils';
buildResponse(undefined, new Error('The user was not found in the db.'));
// {
// success: false,
// data: undefined,
// error: 'The user was not found in the db.'
// }
```
Checking if a value is a response object:
```typescript
import { isResponse } from 'api-response-utils';
isResponse({
success: true,
data: { id: 1, nickname: 'Jane Doe' },
error: undefined,
});
// true
isResponse({ foo: 'bar' });
// false
```
## Types
```typescript
/**
* API Response
* The response object that is sent to the client via the HTTP body.
*/
interface IAPIResponse {
// a response is considered to be successful if error === undefined
success: boolean,
// the data that will be sent to the client (regardless of the request's outcome)
data: T,
// the error thrown during the handling of the request (if any)
error: string | undefined
}
```
## Built With
- TypeScript
## Running the Tests
```bash
npm run test:unit
```
## License
[MIT](https://choosealicense.com/licenses/mit/)