|
| 1 | +# JSON:API Client |
| 2 | + |
| 3 | +A TypeScript client to support [JSON:API](https://jsonapi.org) specification |
| 4 | + |
| 5 | +[](https://www.buymeacoffee.com/stantanasi) |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install @stantanasi/jsonapi-client |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +Create JSON:API model |
| 16 | + |
| 17 | +### JavaScript |
| 18 | + |
| 19 | +```javascript |
| 20 | +import { model, Schema } from '@stantanasi/jsonapi-client'; |
| 21 | + |
| 22 | +export const ArticleSchema = new Schema({ |
| 23 | + attributes: { |
| 24 | + title: {}, |
| 25 | + }, |
| 26 | + |
| 27 | + relationships: { |
| 28 | + author: {}, |
| 29 | + comments: {}, |
| 30 | + }, |
| 31 | +}); |
| 32 | + |
| 33 | + |
| 34 | +export default class Article extends model('articles', ArticleSchema) { } |
| 35 | +``` |
| 36 | + |
| 37 | +### TypeScript |
| 38 | + |
| 39 | +```typescript |
| 40 | +import { model, Schema } from '@stantanasi/jsonapi-client'; |
| 41 | +import Comment from './comment.model'; |
| 42 | +import People from './people.model'; |
| 43 | + |
| 44 | +export interface IArticle { |
| 45 | + id: string; |
| 46 | + |
| 47 | + title: string; |
| 48 | + |
| 49 | + author: People; |
| 50 | + comments: Comment[]; |
| 51 | +} |
| 52 | + |
| 53 | +export const ArticleSchema = new Schema<IArticle>({ |
| 54 | + attributes: { |
| 55 | + title: {}, |
| 56 | + }, |
| 57 | + |
| 58 | + relationships: { |
| 59 | + author: {}, |
| 60 | + comments: {}, |
| 61 | + }, |
| 62 | +}); |
| 63 | + |
| 64 | + |
| 65 | +export default class Article extends model('articles', ArticleSchema) { } |
| 66 | +``` |
| 67 | + |
| 68 | +Use methods |
| 69 | + |
| 70 | +```typescript |
| 71 | +import { connect } from '@stantanasi/jsonapi-client' |
| 72 | + |
| 73 | +connect({ |
| 74 | + baseURL: 'https://example.com', |
| 75 | +}) |
| 76 | + |
| 77 | +const article = new Article({ |
| 78 | + title: 'JSON:API paints my bikeshed!', |
| 79 | +}) |
| 80 | + |
| 81 | +// POST /articles HTTP/1.1 |
| 82 | +await article.save() |
| 83 | +article.id // '1' |
| 84 | + |
| 85 | + |
| 86 | +// GET /articles HTTP/1.1 |
| 87 | +await Article.find() |
| 88 | + |
| 89 | + |
| 90 | +// GET /articles/1 HTTP/1.1 |
| 91 | +await Article.findById('1') |
| 92 | + |
| 93 | + |
| 94 | +// GET /articles/1/comments HTTP/1.1 |
| 95 | +await Article.findById('1').get('comments') |
| 96 | + |
| 97 | + |
| 98 | +// PATCH /articles/1 HTTP/1.1 |
| 99 | +await article.save() |
| 100 | + |
| 101 | + |
| 102 | +// DELETE /articles/1 HTTP/1.1 |
| 103 | +await article.delete() |
| 104 | +``` |
| 105 | + |
| 106 | +Please refer to the [example](./example/README.md) folder to see how to use it |
| 107 | + |
| 108 | +## Contributing |
| 109 | + |
| 110 | +Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. |
| 111 | + |
| 112 | +1. Fork the project |
| 113 | +2. Create your feature branch (`git checkout -b feature/amazing-feature`) |
| 114 | +3. Commit your changes (`git commit -m 'feat: add some amazing feature'`) |
| 115 | +4. Push to the branch (`git push origin feature/amazing-feature`) |
| 116 | +5. Open a pull request |
| 117 | + |
| 118 | +## Author |
| 119 | + |
| 120 | +- [Lory-Stan TANASI](https://github.com/stantanasi) |
| 121 | + |
| 122 | +## License |
| 123 | + |
| 124 | +This project is licensed under the `Apache-2.0` License - see the [LICENSE](LICENSE) file for details |
| 125 | + |
| 126 | +<p align="center"> |
| 127 | + <br /> |
| 128 | + © 2025 Lory-Stan TANASI. All rights reserved |
| 129 | +</p> |
0 commit comments