@pimia/sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +73 -0
- package/dist/api.d.ts +13463 -0
- package/dist/api.js +5 -0
- package/dist/client.d.ts +96 -0
- package/dist/client.js +232 -0
- package/dist/errors.d.ts +57 -0
- package/dist/errors.js +109 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +33 -0
- package/dist/oauth.d.ts +83 -0
- package/dist/oauth.js +147 -0
- package/dist/tokens.d.ts +46 -0
- package/dist/tokens.js +46 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pimia
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# @pimia/sdk
|
|
2
|
+
|
|
3
|
+
Cliente TypeScript oficial de la API de Pimia para **apps de partner**: OAuth
|
|
4
|
+
con PKCE, **rotación del refresh token** persistida, reintentos de rate limit y
|
|
5
|
+
tipos generados del OpenAPI. Licencia MIT.
|
|
6
|
+
|
|
7
|
+
Requisitos: Node ≥ 20 (o cualquier runtime con `fetch` y WebCrypto global).
|
|
8
|
+
|
|
9
|
+
## Instalación
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @pimia/sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
> **Pendiente del primer publish en npm.** El repositorio ya es **público**,
|
|
16
|
+
> así que mientras tanto no hace falta invitación: clona
|
|
17
|
+
> `Pimia-AI/pimia-sdks`, compila este directorio (`npm ci && npm run build`) e
|
|
18
|
+
> instálalo por ruta (`npm install /ruta/a/pimia-sdks/typescript`) o como
|
|
19
|
+
> tarball (`npm pack`). Ojo, `npm install git+https://…` **no** vale: npm
|
|
20
|
+
> instalaría la raíz del monorepo, no `typescript/`. El detalle está en el
|
|
21
|
+
> [README del monorepo](https://github.com/Pimia-AI/pimia-sdks#instalación).
|
|
22
|
+
|
|
23
|
+
## Uso en 20 líneas
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { OAuth, PimiaClient, MemoryTokenStore, SCOPES, createPkceChallenge, createState } from '@pimia/sdk'
|
|
27
|
+
|
|
28
|
+
const config = {
|
|
29
|
+
baseUrl: 'https://acme.pimia.es',
|
|
30
|
+
clientId: process.env.PIMIA_CLIENT_ID!,
|
|
31
|
+
clientSecret: process.env.PIMIA_CLIENT_SECRET, // solo app server-side
|
|
32
|
+
redirectUri: 'https://miapp.example/callback',
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 1. Mandas al usuario a autorizar (guarda verifier y state en su sesión)
|
|
36
|
+
const pkce = await createPkceChallenge()
|
|
37
|
+
const state = createState()
|
|
38
|
+
const url = new OAuth(config).buildAuthorizeUrl({
|
|
39
|
+
scopes: [SCOPES.invoicesRead, SCOPES.customersRead],
|
|
40
|
+
state,
|
|
41
|
+
pkce,
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
// 2. En tu callback: canjeas el code y guardas los tokens en TU store
|
|
45
|
+
const tokens = new MemoryTokenStore() // en producción: tu BD
|
|
46
|
+
tokens.save(await new OAuth(config).exchangeCode(code, pkce))
|
|
47
|
+
|
|
48
|
+
// 3. A partir de aquí, el cliente refresca y reintenta solo
|
|
49
|
+
const pimia = new PimiaClient({ ...config, tokens })
|
|
50
|
+
const invoices = await pimia.invoices.list({ page: 1 })
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Los tipos de todos los endpoints salen del OpenAPI:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import type { paths } from '@pimia/sdk/api'
|
|
57
|
+
|
|
58
|
+
type Invoice = paths['/invoices/{id}']['get']['responses'][200]['content']['application/json']
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Lo único que tienes que leer antes de escribir código
|
|
62
|
+
|
|
63
|
+
**El refresh token de Pimia rota.** Cada refresco devuelve uno nuevo y mata el
|
|
64
|
+
anterior; reusar uno ya rotado revoca el grant entero en cascada. Por eso el
|
|
65
|
+
cliente exige un `TokenStore` en lugar de un string: persiste el conjunto de
|
|
66
|
+
tokens tras cada refresco y no refresques dos veces en paralelo con el mismo
|
|
67
|
+
token. Las dos cosas las cubre el SDK si lo usas como está pensado.
|
|
68
|
+
|
|
69
|
+
## Más
|
|
70
|
+
|
|
71
|
+
Documentación completa, modelo mental (un tenant = una base URL = un token),
|
|
72
|
+
tabla de errores tipados y el contrato OpenAPI, en el monorepo:
|
|
73
|
+
[Pimia-AI/pimia-sdks](https://github.com/Pimia-AI/pimia-sdks).
|