@eduzz/miau-client 1.4.4 → 1.4.5
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/.turbo/turbo-build$colon$types.log +1 -1
- package/README.md +1 -159
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,161 +1,3 @@
|
|
|
1
1
|
# @eduzz/miau-client
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## Instalação
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install @eduzz/miau-client
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Requisitos
|
|
12
|
-
|
|
13
|
-
- Node.js >= 18
|
|
14
|
-
|
|
15
|
-
## Uso
|
|
16
|
-
|
|
17
|
-
```typescript
|
|
18
|
-
import { MiauClient } from '@eduzz/miau-client';
|
|
19
|
-
|
|
20
|
-
const client = new MiauClient({
|
|
21
|
-
apiUrl: 'https://your-miau-api-url',
|
|
22
|
-
appSecret: 'your-app-secret',
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
const token = await client.getToken();
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
## Exemplo
|
|
29
|
-
|
|
30
|
-
```typescript
|
|
31
|
-
import { MiauClient } from '@eduzz/miau-client';
|
|
32
|
-
|
|
33
|
-
const MIAU_API_URL = process.env.MIAU_API_URL!;
|
|
34
|
-
const MIAU_APP_SECRET = process.env.MIAU_APP_SECRET!;
|
|
35
|
-
const YOUR_API_URL = process.env.YOUR_API_URL || 'https://your-api.example.com';
|
|
36
|
-
|
|
37
|
-
const miau = new MiauClient({ apiUrl: MIAU_API_URL, appSecret: MIAU_APP_SECRET });
|
|
38
|
-
const token = await miau.getToken();
|
|
39
|
-
|
|
40
|
-
const response = await fetch(`${YOUR_API_URL}/your/endpoint`, {
|
|
41
|
-
headers: { Authorization: `Bearer ${token}` },
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
const data = await response.json();
|
|
45
|
-
console.log(JSON.stringify(data, null, 2));
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
## Middleware Express
|
|
49
|
-
|
|
50
|
-
O client inclui um middleware Express que autentica requisições usando tokens Miau e verifica permissões automaticamente.
|
|
51
|
-
|
|
52
|
-
```typescript
|
|
53
|
-
import express from 'express';
|
|
54
|
-
import { MiauClient } from '@eduzz/miau-client';
|
|
55
|
-
|
|
56
|
-
const app = express();
|
|
57
|
-
const miau = new MiauClient({ apiUrl: MIAU_API_URL, appSecret: MIAU_APP_SECRET });
|
|
58
|
-
|
|
59
|
-
app.use(miau.middleware());
|
|
60
|
-
|
|
61
|
-
app.get('/your/endpoint', (req, res) => {
|
|
62
|
-
// req.miauApplication - { id: '...', name: '...' }
|
|
63
|
-
// req.miauMetadata - metadata de permissão
|
|
64
|
-
res.json({ app: req.miauApplication });
|
|
65
|
-
});
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
### Handler de fallback
|
|
69
|
-
|
|
70
|
-
O middleware aciona o fallback quando o token está ausente ou não é um token Miau válido (erros HTTP 400). Isso permite lidar com esquemas de autenticação alternativos nas mesmas rotas -- por exemplo, aceitar Basic Auth para clients legados enquanto ainda suporta tokens Miau.
|
|
71
|
-
|
|
72
|
-
```typescript
|
|
73
|
-
import express, { type Request, type Response, type NextFunction } from 'express';
|
|
74
|
-
import { MiauClient } from '@eduzz/miau-client';
|
|
75
|
-
|
|
76
|
-
const app = express();
|
|
77
|
-
const miau = new MiauClient({ apiUrl: MIAU_API_URL, appSecret: MIAU_APP_SECRET });
|
|
78
|
-
|
|
79
|
-
const basicAuthFallback = (req: Request, res: Response, next: NextFunction) => {
|
|
80
|
-
const authHeader = req.headers.authorization || '';
|
|
81
|
-
|
|
82
|
-
if (!authHeader.startsWith('Basic ')) {
|
|
83
|
-
res.status(401).json({ error: 'Unauthorized', message: 'Credenciais não fornecidas' });
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const [username, password] = Buffer.from(authHeader.slice(6), 'base64').toString().split(':');
|
|
88
|
-
|
|
89
|
-
// Valide as credenciais com sua própria lógica
|
|
90
|
-
if (!validateCredentials(username, password)) {
|
|
91
|
-
res.status(401).json({ error: 'Unauthorized', message: 'Credenciais inválidas' });
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
req.username = username;
|
|
96
|
-
next();
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
app.use('/legacy-route', miau.middleware({ fallbackMiddleware: basicAuthFallback }));
|
|
100
|
-
|
|
101
|
-
app.get('/legacy-route', (req, res) => {
|
|
102
|
-
if (req.miauApplication) {
|
|
103
|
-
// Autenticado via token Miau
|
|
104
|
-
res.json({ auth: 'miau', application: req.miauApplication });
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Autenticado via fallback Basic Auth
|
|
109
|
-
res.json({ auth: 'basic', username: req.username });
|
|
110
|
-
});
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
## Hook Fastify
|
|
114
|
-
|
|
115
|
-
O client também oferece um hook `preHandler` para Fastify com o mesmo fluxo de autenticação.
|
|
116
|
-
|
|
117
|
-
```typescript
|
|
118
|
-
import Fastify from 'fastify';
|
|
119
|
-
import { MiauClient } from '@eduzz/miau-client';
|
|
120
|
-
|
|
121
|
-
const app = Fastify();
|
|
122
|
-
const miau = new MiauClient({ apiUrl: MIAU_API_URL, appSecret: MIAU_APP_SECRET });
|
|
123
|
-
|
|
124
|
-
app.addHook('preHandler', miau.hook());
|
|
125
|
-
|
|
126
|
-
app.get('/your/endpoint', async (request, reply) => {
|
|
127
|
-
// request.miauApplication - { id: '...', name: '...' }
|
|
128
|
-
// request.miauMetadata - metadata de permissão
|
|
129
|
-
return { app: request.miauApplication };
|
|
130
|
-
});
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
### Handler de fallback
|
|
134
|
-
|
|
135
|
-
```typescript
|
|
136
|
-
import Fastify from 'fastify';
|
|
137
|
-
import { MiauClient } from '@eduzz/miau-client';
|
|
138
|
-
|
|
139
|
-
const app = Fastify();
|
|
140
|
-
const miau = new MiauClient({ apiUrl: MIAU_API_URL, appSecret: MIAU_APP_SECRET });
|
|
141
|
-
|
|
142
|
-
const basicAuthFallback = async (request: FastifyRequest, reply: FastifyReply) => {
|
|
143
|
-
const authHeader = request.headers.authorization || '';
|
|
144
|
-
|
|
145
|
-
if (!authHeader.startsWith('Basic ')) {
|
|
146
|
-
reply.code(401).send({ error: 'Unauthorized', message: 'Credenciais não fornecidas' });
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const [username, password] = Buffer.from(authHeader.slice(6), 'base64').toString().split(':');
|
|
151
|
-
|
|
152
|
-
if (!validateCredentials(username, password)) {
|
|
153
|
-
reply.code(401).send({ error: 'Unauthorized', message: 'Credenciais inválidas' });
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
request.username = username;
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
app.addHook('preHandler', miau.hook({ fallbackMiddleware: basicAuthFallback }));
|
|
161
|
-
```
|
|
3
|
+
[Documentação](https://github.com/eduzz/eduzz-miau/blob/master/docs/node-client.md)
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eduzz/miau-client",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.5",
|
|
4
4
|
"description": "Eduzz Miau Client",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"lint": "eslint
|
|
8
|
+
"lint": "eslint",
|
|
9
|
+
"typecheck": "tsc --noEmit",
|
|
9
10
|
"test": "tsx --test src/functions.test.ts",
|
|
10
11
|
"build": "esbuild src/index.ts --bundle --sourcemap --platform=node --target=es2020 --outfile=dist/index.js",
|
|
11
12
|
"build:types": "tsc --emitDeclarationOnly --outDir dist"
|