Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions packages/axios/src/api/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ export class Request {
* Get the axios client.
*/
get axios (): AxiosInstance {
this.repository.axios = this.repository.axios ?? this.repository.config.axiosApi.axios
if (!this.repository.axios) {
const axios = this.repository.axios
if (!axios) {
throw new Error(
'[Pinia ORM Axios] The axios instance is not registered. Please register the axios instance to the repository.',
)
}

return this.repository.axios
return axios
}

/**
Expand Down
34 changes: 23 additions & 11 deletions packages/axios/src/repository/AxiosRepository.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import type { Database, Model } from 'pinia-orm'
import type { Pinia } from 'pinia'
import { Repository, config } from 'pinia-orm'
import type { Model } from 'pinia-orm'
import { Repository } from 'pinia-orm'
import type { AxiosInstance } from 'axios'
import { useAxiosApi } from '../index'
import type { Config, GlobalConfig } from '../types/config'

export class AxiosRepository<M extends Model = Model> extends Repository<M> {
axios: AxiosInstance
globalApiConfig: GlobalConfig
apiConfig: Config
apiConfig: Config = {}

constructor (database: Database, pinia?: Pinia) {
super(database, pinia)
this.axios = config?.axiosApi?.axios || null
this.globalApiConfig = config?.axiosApi || {}
this.apiConfig = {}
protected axiosInstance: AxiosInstance | null = null

/**
* The axios instance from the plugin config, unless an instance was set
* explicitly via `setAxios`. The plugin config is resolved lazily because
* plugins are registered after the repository has been constructed.
*/
get axios (): AxiosInstance | null {
return this.axiosInstance ?? this.globalApiConfig.axios ?? null
}

set axios (axios: AxiosInstance | null) {
this.axiosInstance = axios
}

/**
* The global plugin config passed to `createPiniaOrmAxios`.
*/
get globalApiConfig (): GlobalConfig {
return this.config.axiosApi ?? {}
}

api () {
Expand Down
66 changes: 66 additions & 0 deletions packages/axios/test/feature/Request_GlobalConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import { createPinia, setActivePinia } from 'pinia'
import { createApp } from 'vue'
import { Model, config, createORM } from 'pinia-orm'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { assertState } from '../helpers'
import { createPiniaOrmAxios, useAxiosRepo } from '../../src'

describe('Feature - Request - Global Config', () => {
let mock: MockAdapter

class User extends Model {
static entity = 'users'

static fields () {
return {
id: this.attr(null),
name: this.attr(''),
}
}
}

beforeEach(() => {
mock = new MockAdapter(axios)
})
afterEach(() => {
mock.reset()
})

it('applies global plugin options like baseURL and dataKey on first repository use', async () => {
// Simulate a freshly booted app where no repository has been used yet,
// so the axios plugin has not been applied to the global config.
delete config.axiosApi

const app = createApp({})
const pinia = createPinia()
pinia.use(createORM({
plugins: [
createPiniaOrmAxios({
axios,
baseURL: 'https://example.com/api',
dataKey: 'data',
}),
],
}))
app.use(pinia)
setActivePinia(pinia)

mock.onGet('https://example.com/api/users').reply(200, {
data: [{ id: 1, name: 'John Doe' }],
})

const userStore = useAxiosRepo(User)

await userStore.api().get('/users')

expect(mock.history.get[0].baseURL).toBe('https://example.com/api')

assertState({
users: {
1: { id: 1, name: 'John Doe' },
},
})
})
})
Loading