@useflagly/sdk-javascript 0.1.1
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/README.md +63 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +80 -0
- package/package.json +29 -0
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# @useflagly/sdk-javascript
|
|
2
|
+
|
|
3
|
+
SDK JavaScript para interagir com as rotas expostas pelo `SystemModule` (FeatureFlag API).
|
|
4
|
+
|
|
5
|
+
## Instalação
|
|
6
|
+
|
|
7
|
+
Configurar publicação/registro para o GitHub Packages (ex.: `useflagly`):
|
|
8
|
+
|
|
9
|
+
.npmrc (no repositório ou no CI):
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
@useflagly:registry=https://npm.pkg.github.com
|
|
13
|
+
//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Para instalar em outro projeto (após publicar):
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
npm install @useflagly/sdk-javascript
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Uso
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import { SystemSDK } from '@featureflag/sdk-system';
|
|
26
|
+
|
|
27
|
+
const sdk = new SystemSDK({ baseUrl: 'https://api.example.com', token: process.env.API_TOKEN });
|
|
28
|
+
|
|
29
|
+
// Validar flag
|
|
30
|
+
await sdk.validateFlag('my-flag-slug', { cpf: '00000000000', context: { userId: '123' } }, 'PRD');
|
|
31
|
+
|
|
32
|
+
// Ler cache de flag
|
|
33
|
+
await sdk.getFlagCache('my-flag-slug', '00000000000');
|
|
34
|
+
|
|
35
|
+
// Inicializar (producer)
|
|
36
|
+
await sdk.initializeFlags({ pattern: '...' });
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Métodos principais
|
|
40
|
+
|
|
41
|
+
- `validateFlag(slug, body, environment?)`
|
|
42
|
+
- `getFlagCache(slug, cpf?)`
|
|
43
|
+
- `validateFlow(slug, body, environment?)`
|
|
44
|
+
- `getFlowCache(slug, cpf?)`
|
|
45
|
+
- `validateFlowPart(...)`
|
|
46
|
+
- `getFlowPartCache(...)`
|
|
47
|
+
- `validateScenario(...)`
|
|
48
|
+
- `getScenarioCache(...)`
|
|
49
|
+
- `initializeFlags(payload, environment?)`
|
|
50
|
+
- `getResult(cpf)`
|
|
51
|
+
|
|
52
|
+
## Publicação no GitHub Packages
|
|
53
|
+
|
|
54
|
+
1. Crie um Personal Access Token com `write:packages` e `read:packages` (scope).
|
|
55
|
+
2. Adicione o token como `GITHUB_TOKEN` no CI ou no `~/.npmrc` local.
|
|
56
|
+
3. Ajuste `package.json` `name` e `repository.url` se necessário (já configurado para `useflagly`).
|
|
57
|
+
4. Rodar:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npm publish --access=restricted
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
> Observação: para pacotes sob escopo de organização, garanta que o registro do escopo está apontado para `https://npm.pkg.github.com` no `.npmrc`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export type ValidateBody = {
|
|
2
|
+
identifier?: string;
|
|
3
|
+
context?: Record<string, any>;
|
|
4
|
+
};
|
|
5
|
+
export type ReceiveMessage = {
|
|
6
|
+
identifier: string;
|
|
7
|
+
slug: string;
|
|
8
|
+
companyId?: number;
|
|
9
|
+
context?: Record<string, any>;
|
|
10
|
+
};
|
|
11
|
+
export type SDKOptions = {
|
|
12
|
+
baseUrl: string;
|
|
13
|
+
token?: string;
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
};
|
|
16
|
+
export declare class SystemSDK {
|
|
17
|
+
private baseUrl;
|
|
18
|
+
private token?;
|
|
19
|
+
constructor(opts: SDKOptions);
|
|
20
|
+
private headers;
|
|
21
|
+
private request;
|
|
22
|
+
validateFlag(slug: string, body: ValidateBody, environment?: string): Promise<any>;
|
|
23
|
+
getFlagCache(slug: string, identifier?: string): Promise<any>;
|
|
24
|
+
validateFlow(slug: string, body: ValidateBody, environment?: string): Promise<any>;
|
|
25
|
+
getFlowCache(slug: string, identifier?: string): Promise<any>;
|
|
26
|
+
validateFlowPart(slug: string, body: ValidateBody, environment?: string): Promise<any>;
|
|
27
|
+
getFlowPartCache(slug: string, identifier?: string): Promise<any>;
|
|
28
|
+
validateScenario(slug: string, body: ValidateBody, environment?: string): Promise<any>;
|
|
29
|
+
getScenarioCache(slug: string, identifier?: string): Promise<any>;
|
|
30
|
+
initializeFlags(payload: ReceiveMessage, environment?: string): Promise<any>;
|
|
31
|
+
getResult(cpf: string): Promise<any>;
|
|
32
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SystemSDK = void 0;
|
|
7
|
+
const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
|
8
|
+
class SystemSDK {
|
|
9
|
+
constructor(opts) {
|
|
10
|
+
this.baseUrl = opts.baseUrl.replace(/\/+$/, '');
|
|
11
|
+
this.token = opts.token;
|
|
12
|
+
}
|
|
13
|
+
headers(env) {
|
|
14
|
+
const h = {
|
|
15
|
+
'content-type': 'application/json',
|
|
16
|
+
};
|
|
17
|
+
if (this.token)
|
|
18
|
+
h['authorization'] = `Bearer ${this.token}`;
|
|
19
|
+
if (env)
|
|
20
|
+
h['environment'] = env;
|
|
21
|
+
return h;
|
|
22
|
+
}
|
|
23
|
+
async request(path, method = 'GET', body, env) {
|
|
24
|
+
const res = await (0, cross_fetch_1.default)(this.baseUrl + path, {
|
|
25
|
+
method,
|
|
26
|
+
headers: this.headers(env),
|
|
27
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
28
|
+
});
|
|
29
|
+
if (res.status === 204)
|
|
30
|
+
return null;
|
|
31
|
+
const text = await res.text();
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(text);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return text;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// Flags
|
|
40
|
+
async validateFlag(slug, body, environment) {
|
|
41
|
+
return this.request(`/validate/flag/${encodeURIComponent(slug)}`, 'POST', body, environment);
|
|
42
|
+
}
|
|
43
|
+
async getFlagCache(slug, identifier) {
|
|
44
|
+
const q = identifier ? `?identifier=${encodeURIComponent(identifier)}` : '';
|
|
45
|
+
return this.request(`/validate/flag/${encodeURIComponent(slug)}${q}`, 'GET');
|
|
46
|
+
}
|
|
47
|
+
// Flow
|
|
48
|
+
async validateFlow(slug, body, environment) {
|
|
49
|
+
return this.request(`/validate/flow/${encodeURIComponent(slug)}`, 'POST', body, environment);
|
|
50
|
+
}
|
|
51
|
+
async getFlowCache(slug, identifier) {
|
|
52
|
+
const q = identifier ? `?identifier=${encodeURIComponent(identifier)}` : '';
|
|
53
|
+
return this.request(`/validate/flow/${encodeURIComponent(slug)}${q}`, 'GET');
|
|
54
|
+
}
|
|
55
|
+
// Flow part
|
|
56
|
+
async validateFlowPart(slug, body, environment) {
|
|
57
|
+
return this.request(`/validate/flow-part/${encodeURIComponent(slug)}`, 'POST', body, environment);
|
|
58
|
+
}
|
|
59
|
+
async getFlowPartCache(slug, identifier) {
|
|
60
|
+
const q = identifier ? `?identifier=${encodeURIComponent(identifier)}` : '';
|
|
61
|
+
return this.request(`/validate/flow-part/${encodeURIComponent(slug)}${q}`, 'GET');
|
|
62
|
+
}
|
|
63
|
+
// Scenario
|
|
64
|
+
async validateScenario(slug, body, environment) {
|
|
65
|
+
return this.request(`/validate/scenario/${encodeURIComponent(slug)}`, 'POST', body, environment);
|
|
66
|
+
}
|
|
67
|
+
async getScenarioCache(slug, identifier) {
|
|
68
|
+
const q = identifier ? `?identifier=${encodeURIComponent(identifier)}` : '';
|
|
69
|
+
return this.request(`/validate/scenario/${encodeURIComponent(slug)}${q}`, 'GET');
|
|
70
|
+
}
|
|
71
|
+
// Initialize (producer)
|
|
72
|
+
async initializeFlags(payload, environment) {
|
|
73
|
+
return this.request(`/validate/initialize`, 'POST', payload, environment);
|
|
74
|
+
}
|
|
75
|
+
// Result
|
|
76
|
+
async getResult(cpf) {
|
|
77
|
+
return this.request(`/validate/result/${encodeURIComponent(cpf)}`, 'GET');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.SystemSDK = SystemSDK;
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@useflagly/sdk-javascript",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "JavaScript SDK para SystemModule (FeatureFlag API)",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": ["dist"],
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"registry": "https://registry.npmjs.org",
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc -p tsconfig.json",
|
|
14
|
+
"clean": "rm -rf dist",
|
|
15
|
+
"prepublishOnly": "npm run build"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/useflagly/backend.git"
|
|
20
|
+
},
|
|
21
|
+
"author": "",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"cross-fetch": "^3.1.8"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"typescript": "^5.9.3"
|
|
28
|
+
}
|
|
29
|
+
}
|