Services

createAxiosAdapter

Factory per l'AttachmentAdapter axios di default. Supporta storagePath, basePath custom e mapper per backend con schema legacy.

Panoramica

createAxiosAdapter è la factory ufficiale per ottenere un AttachmentAdapter basato su axios. Riceve una AxiosInstance (con i tuoi interceptors di auth/retry/tracing) e ritorna un adapter pronto da passare al VueAttachmentPlugin o a useAttachments.

function createAxiosAdapter(
  http: AxiosInstance,
  options?: CreateAxiosAdapterOptions
): AttachmentAdapter

Opzioni

interface CreateAxiosAdapterOptions {
  /** Storage path applicato come query param a download/delete/admin endpoints. */
  storagePath?: string

  /** Path base degli endpoint REST. Default: '/attachments'. */
  basePath?: string

  /** Mapper per backend con schema diverso da Attachment (camelCase). */
  mapper?: AttachmentResponseMapper
}

interface AttachmentResponseMapper {
  toAttachment(raw: unknown): Attachment
  fromAttachment?(attachment: Attachment): unknown
}

Endpoint chiamati

I metodi puntano a path relativi al baseURL di axios + basePath (default /attachments):

MetodoHTTPPath
list(query)GET/?<querystring>
get(id)GET/:id
upload(params)POST/ (multipart/form-data)
download(id, disposition)GET/:id/download?disposition=...
delete(id)DELETE/:id
deleteMany(ids)POST/delete (body: { ids })
findOrphaned()GET/admin/orphaned
cleanOrphaned(dryRun)POST/admin/cleanup?dryRun=...

Formato response

L'adapter accetta sia il formato wrapped PZeta sia il payload diretto:

// Wrapped (PZeta standard)
{ "success": true, "data": { /* attachment | attachment[] | ... */ } }

// Diretto
{ /* attachment */ }

Il fallback è automatico: se il payload ha { success: true, data } viene unwrap, altrimenti è usato as-is.

Esempio base

import axios from 'axios'
import { createAxiosAdapter } from '@pzeta/vue-attachment'

const http = axios.create({
  baseURL: '/api/storage',
  timeout: 30_000
})

http.interceptors.request.use((config) => {
  const token = localStorage.getItem('token')
  if (token) config.headers.Authorization = `Bearer ${token}`
  return config
})

const adapter = createAxiosAdapter(http, {
  storagePath: 'attachments/tickets'
})

basePath custom

Se i tuoi endpoint vivono sotto un prefisso diverso da /attachments (es. /files, /uploads):

const adapter = createAxiosAdapter(http, {
  basePath: '/files',
  storagePath: 'tickets/2026'
})
// → GET /files, POST /files, DELETE /files/:id, ...

Mapper per backend legacy

Se il backend non restituisce direttamente Attachment (camelCase) — ad esempio uno schema PZeta legacy con campi italiani snake_case — passa un mapper.toAttachment:

import { createAxiosAdapter } from '@pzeta/vue-attachment'

interface RawAllegato {
  idallegato: string
  nomefile: string
  dimensione: number
  mime_type: string
  datacaricamento: string
  thumbnail_url?: string
}

const adapter = createAxiosAdapter(http, {
  mapper: {
    toAttachment: (raw: unknown) => {
      const r = raw as RawAllegato
      return {
        id: r.idallegato,
        fileName: r.nomefile,
        fileType: r.mime_type,
        size: r.dimensione,
        uploadDate: r.datacaricamento,
        thumbnailUrl: r.thumbnail_url
      }
    }
  }
})
Il mapper viene applicato a list(), get() e upload(). Il download() ritorna sempre Blob raw (responseType blob), non passa per il mapper.

Esempio: download con save-as

L'adapter ritorna il Blob; il composable useAttachments triggera già il save dialog. Se vuoi farlo manualmente:

const blob = await adapter.download('abc123', 'attachment')
const url = URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = 'contratto.pdf'
link.click()
URL.revokeObjectURL(url)

Uso completo (plugin + componenti)

// main.ts
import { createApp } from 'vue'
import axios from 'axios'
import { VueAttachmentPlugin, createAxiosAdapter } from '@pzeta/vue-attachment'
import '@pzeta/vue-attachment/style.css'

const http = axios.create({ baseURL: '/api/storage' })
http.interceptors.request.use(addAuthHeader)

createApp(App)
  .use(VueAttachmentPlugin, {
    adapter: createAxiosAdapter(http, {
      storagePath: 'attachments/tickets',
      basePath: '/attachments'
    })
  })
  .mount('#app')

Classe AxiosAttachmentAdapter

createAxiosAdapter è una factory che istanzia internamente la classe AxiosAttachmentAdapter. Per scenari avanzati (estensione, override di metodi) puoi importarla direttamente:

import { AxiosAttachmentAdapter } from '@pzeta/vue-attachment'

class TenantAttachmentAdapter extends AxiosAttachmentAdapter {
  // override custom
}

TypeScript

Tutti i tipi rilevanti sono esportati dal package root:

import type {
  AttachmentAdapter,
  AttachmentResponseMapper,
  AxiosAttachmentAdapterOptions,
  CreateAxiosAdapterOptions
} from '@pzeta/vue-attachment'