|
| 1 | +# create-api |
| 2 | +create-api is a library to create a REST client with support for CRUD operations and dynamic finders. |
| 3 | + |
| 4 | + |
| 5 | +Give the sample data below and served via json-server. Let's take a look on how to query the data via create-api. |
| 6 | + |
| 7 | +| ID | Brand | Name | Warranty | Instock | RunFlat | Price | |
| 8 | +|--- |--- |--- | --- |--- |--- |--- | |
| 9 | +| 1 | Michelin | Defender | 50000 | true | true | 125.99 | |
| 10 | +| 2 | Michelin | Premier A/S | 50000 | true | true | 155.99 | |
| 11 | +| 3 | Michelin | Energy Saver | 20000 | true | false | 129.99 | |
| 12 | +| 4 | Michelin | Primacy MXM4 | 20000 | false | true | 154.99 | |
| 13 | +| 5 | Michelin | Primacy MXV4 | 80000 | false | false | 154.99 | |
| 14 | +| 6 | Fuzion | Touring | 50000 | true | false | 65.99 | |
| 15 | +| 7 | Fuzion | UHP | 50000 | true | true | 50.00 | |
| 16 | +| 8 | Goodyear | Eagle LS | 40000 | true | true | 129.99 | |
| 17 | +| 9 | Goodyear | Wrangler Radial | 50000 | false | true | 125.99 | |
| 18 | +| 10 | Goodyear | Assurance All Season | 80000 | true | false | 115.99 | |
| 19 | + |
| 20 | +~~~~javascript |
| 21 | +let api = createApi('http://localhost:3002/tires') |
| 22 | +// regular CRUD operations |
| 23 | +api.findAll() // returns all data |
| 24 | +api.find(1) // return data with id of 1 |
| 25 | +api.create({ brand: 'Firestone', name: 'Winterforce', ...rest of data }) // create a new object |
| 26 | +api.update(1, { name: 'Updated Name' }) //updates name of object with id of 1 |
| 27 | +api.delete(1) // deletes object with id of 1 |
| 28 | + |
| 29 | +// dynamic finders |
| 30 | +api.findByBrand('Michelin') // translates query to ?brand=Michelin |
| 31 | +api.findByBrandAndInStock('Michelin', true) // translates query to ?brand=Michelin&instock=true |
| 32 | +api.findByWarranty({gte: 40000, lte: 60000}) //translates query to ?warranty_gte=40000&warranty_lte=60000 |
| 33 | +api.findByBrandAndWarranty('Michelin', {gte: 40000, lte: 60000}) //translates query to ?brand=Michelin&warranty_gte=40000&warranty_lte=60000 |
0 commit comments