Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e3d9bc3
chore: bump decode-uri-component from 0.2.0 to 0.2.2
dependabot[bot] Dec 3, 2022
b293bdc
Merge pull request #257 from robsontenorio/dependabot/npm_and_yarn/de…
JoaoPedroAS51 Dec 7, 2022
f14eebd
chore: bump qs from 6.9.4 to 6.9.7 (#258)
dependabot[bot] May 4, 2023
51a1c8b
chore: bump json5 from 1.0.1 to 1.0.2 (#262)
dependabot[bot] May 4, 2023
f055163
chore: bump http-cache-semantics from 4.1.0 to 4.1.1 (#269)
dependabot[bot] May 4, 2023
74085bb
chore: bump http-cache-semantics from 4.1.0 to 4.1.1 in /docs (#268)
dependabot[bot] May 4, 2023
1d7157b
chore: bump ua-parser-js from 0.7.28 to 0.7.33 in /docs (#267)
dependabot[bot] May 4, 2023
b19fd0d
chore: bump json5 from 1.0.1 to 1.0.2 in /docs (#263)
dependabot[bot] May 4, 2023
ec2db06
chore: bump express from 4.17.1 to 4.18.2 in /docs (#260)
dependabot[bot] May 4, 2023
88fbe9a
chore: bump decode-uri-component from 0.2.0 to 0.2.2 in /docs (#259)
dependabot[bot] May 4, 2023
115a850
Added wrap and _unwrap methods to Model.js to mimmic Laravel's resour…
JRogaishio Oct 4, 2024
9ad109b
Lock the defu package to 6.1.2. Version 6.1.3+ has a regression that …
JRogaishio Oct 4, 2024
aaad116
Added unit tests for the new wrap() method
JRogaishio Oct 4, 2024
1ad80d5
Added types and docs for new wrap() method
JRogaishio Oct 4, 2024
e2921f6
Merge pull request #1 from mindedge/feat/resource-wrapper
JRogaishio Oct 4, 2024
6d26eae
feat(model): Added wrap and _unwrap methods to Model.js to mimmic Lar…
JRogaishio Oct 4, 2024
385fa93
feat(fix): Lock the defu package to 6.1.2. Version 6.1.3+ has a regre…
JRogaishio Oct 4, 2024
8815a00
test(model): Added unit tests for the new wrap() method
JRogaishio Oct 4, 2024
602a787
docs(model): Added types and docs for new wrap() method
JRogaishio Oct 4, 2024
d00e84a
feat(*): Updated package name for deployments
JRogaishio Oct 4, 2024
8351981
fix: Github tokens cannot be prefixed with GITHUB_
JRogaishio Oct 4, 2024
7837d2a
feat(fix): Lock the defu package to 6.1.2. Version 6.1.3+ has a regre…
JRogaishio Oct 4, 2024
9199b7a
Merge remote-tracking branch 'origin/feat/resource-wrapper' into dev
JRogaishio Oct 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ jobs:
- name: Release
run: yarn semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

docs:
Expand Down Expand Up @@ -171,5 +171,5 @@ jobs:
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
github_token: ${{ secrets.GH_TOKEN }}
publish_dir: ./docs/dist
36 changes: 34 additions & 2 deletions docs/content/en/api/model-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ category: API

## Global Options

It's recommended to define the global options in your [Base Model](/configuration#creating-a-base-model),
It's recommended to define the global options in your [Base Model](/configuration#creating-a-base-model),
in order to abstract configuration from your models.

### `$http`

- Returns: `HTTP Client Instance`

Instance of the HTTP client which is used to make requests.

See [Installation](/installation)

### `baseURL`

- Returns: `string`

Base URL which is used and prepended to make requests.
Expand All @@ -31,6 +33,7 @@ baseURL() {
```

### `request`

- Arguments: `(config)`
- Returns: `HTTP Client Request`

Expand All @@ -45,6 +48,7 @@ request(config) {
```

### `parameterNames`

- Returns: `object`

This method can be overridden in the model to customize the name of the query parameters.
Expand All @@ -66,34 +70,42 @@ parameterNames() {
```

#### `include`

- Default: `include`
- Returns: `string`

#### `filter`

- Default: `filter`
- Returns: `string`

#### `sort`

- Default: `sort`
- Returns: `string`

#### `fields`

- Default: `fields`
- Returns: `string`

#### `append`

- Default: `append`
- Returns: `string`

#### `page`

- Default: `page`
- Returns: `string`

#### `limit`

- Default: `limit`
- Returns: `string`

### `formData`

- Returns: `object`

This method can be overridden in the model to configure `object-to-formdata`.
Expand All @@ -112,6 +124,7 @@ formData() {
```

### `stringifyOptions`

- Default: `{ encode: false, arrayFormat: 'comma' }`
- Returns: `object`

Expand All @@ -133,6 +146,7 @@ stringifyOptions() {
These are model-related options.

### `resource`

- Returns: `string`

Resource route of the model which is used to build the query.
Expand All @@ -146,6 +160,7 @@ resource() {
```

### `primaryKey`

- Default: `id`
- Returns: `string`

Expand All @@ -159,10 +174,26 @@ primaryKey() {
}
```

### `wrap`

- Default: `null`
- Returns: `string`

The "data" wrapper that should be checked when retrieving models.

See [Configuration](/configuration#changing-the-wrapper)

```js
wrap() {
return 'data'
}
```

### `relations`

- Returns: `object`

This method can be implemented in the model to apply model instances to eager loaded relationships.
This method can be implemented in the model to apply model instances to eager loaded relationships.
It works for collections too.

It must return an object, which the key is the property of the relationship, and the value is the
Expand All @@ -179,6 +210,7 @@ relations() {
```

### `hasMany`

- Arguments: `(model)`
- Returns: `Model`

Expand Down
61 changes: 47 additions & 14 deletions docs/content/en/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ category: Getting Started

See the [API reference](/api/model-options) for a list of available options.

The first step is to create a base model to define the default options, in order to abstract configuration
from your models. It should extend the
The first step is to create a base model to define the default options, in order to abstract configuration
from your models. It should extend the
[Base Model](https://github.com/robsontenorio/vue-api-query/blob/master/src/Model.js) of [vue-api-query](https://github.com/robsontenorio/vue-api-query).

The base model must implement two methods:

- `baseURL` - The base url of your REST API.
- `request` - The default request method.

Expand Down Expand Up @@ -43,9 +44,11 @@ export default class Model extends BaseModel {
Now let's create our domain models that extends the base model. We can create as many models as we like.

Each model must implement:

- `resource` - The resource route of the model.

We can create a **User** model like this:

```js{}[~/models/User.js]
import Model from './Model'

Expand All @@ -71,7 +74,7 @@ export default class User extends Model {
}

// Computed properties are reactive -> user.fullName
// Make sure to use "get" prefix
// Make sure to use "get" prefix
get fullName () {
return `${this.firstname} ${this.lastname}`
}
Expand All @@ -90,6 +93,7 @@ export default class User extends Model {
If we are working on a Typescript project, we can infer the types of the fields, so we have intellisense.

#### Directly in Model

```ts{}[~/models/User.ts]
import Model from './Model'

Expand All @@ -103,6 +107,7 @@ export default class User extends Model {
```

#### Using an Interface

```ts{}[~/models/User.ts]
import Model from './Model'

Expand Down Expand Up @@ -149,6 +154,34 @@ export default class Post extends Model {

This **Post** model will build the query using the `slug` as primary key: `/posts/{slug}`

## Changing the Wrapper

<alert type="info">By default, the `wrap` is set to `null`.</alert>

See the [API reference](/api/model-options#wrap)

It's possible to change the wrapper of a model response by implementing the `wrap` method.
This way, the specified key will be used to unwrap the data.

Let's create a **Post** model and set its wrap key to `data`.

```js{}[~/models/Post.js]
import Model from './Model'

export default class Post extends Model {
// Set the resource route of the model
resource() {
return 'posts'
}

// Define the primary key of the model
wrap() {
return 'data'
}
```

This **Post** model will now look inside the API response top level `data` property when retrieving data and initializing your models.

## Defining Relationships

It's also possible to define the relationships of our models. By doing this, model instances will be automatically
Expand All @@ -158,7 +191,7 @@ applied to relationships, giving you access to all of their features.

See the [API reference](/api/model-options#relations)

For relationships that have been eager loaded, we only need to implement the `relations` method
For relationships that have been eager loaded, we only need to implement the `relations` method
to apply their model instances. It works for collections too.

The `relations` method must return an object, which the key is the property of the relationship, and the value is the
Expand Down Expand Up @@ -191,7 +224,7 @@ export default class Post extends Model {
}
```

Now we can easily access an instance of the **User** model containing the eager loaded data
Now we can easily access an instance of the **User** model containing the eager loaded data
using the specified key: `post.user`

The `relations` method also support nested keys, by dot notation:
Expand Down Expand Up @@ -247,7 +280,7 @@ export default class User extends Model {
}

// Computed properties are reactive -> user.fullName
// Make sure to use "get" prefix
// Make sure to use "get" prefix
get fullName () {
return `${this.firstname} ${this.lastname}`
}
Expand Down Expand Up @@ -290,7 +323,7 @@ export default class Model extends BaseModel {
const customParams = {
include: 'include_custom'
}

return { ...defaultParams, ...customParams }
}
}
Expand All @@ -300,10 +333,10 @@ export default class Model extends BaseModel {

See the [API reference](/api/model-options#stringifyOptions) and [qs](https://github.com/ljharb/qs#stringifying)

We may also need to configure the parser to match our needs. By default, it is configured to match
We may also need to configure the parser to match our needs. By default, it is configured to match
`spatie/laravel-query-builder`, which uses `comma` array format.

If we want, for example, to change this behaviour to `indices`, we can configure the stringify options of `qs`
If we want, for example, to change this behaviour to `indices`, we can configure the stringify options of `qs`
by overriding the `stringifyOptions` method.

We can globally configure this in the [Base Model](/configuration#creating-a-base-model):
Expand All @@ -329,10 +362,10 @@ export default class Model extends BaseModel {
const customParams = {
include: 'include_custom'
}

return { ...defaultParams, ...customParams }
}

// Configure qs
stringifyOptions() {
return {
Expand Down Expand Up @@ -373,17 +406,17 @@ export default class Model extends BaseModel {
const customParams = {
include: 'include_custom'
}

return { ...defaultParams, ...customParams }
}

// Configure qs
stringifyOptions() {
return {
arrayFormat: 'indices'
}
}

// Configure object-to-formadata
formData() {
return {
Expand Down
Loading
Loading