@tranzakpackage/cards-server 0.0.0-stage → 2.0.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/README.md +10 -2
- package/package.json +17 -4
- package/src/index.d.ts +28 -0
- package/src/index.js +64 -0
package/README.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Tranzak Cards - server
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Create widget sessions from your server (Node.js, Next.js route handlers and server actions). Your API key never leaves your server.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @tranzakpackage/cards-server
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Full documentation and API reference: https://docs.tranzak.co (Cards section).
|
|
10
|
+
|
|
11
|
+
Proprietary software of Lomotec LLC. All rights reserved.
|
package/package.json
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tranzakpackage/cards-server",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Tranzak Cards for your server (Node.js, Next.js route handlers and server actions): create widget sessions. Your API key never leaves your server.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"module": "src/index.js",
|
|
8
|
+
"types": "src/index.d.ts",
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"license": "UNLICENSED",
|
|
11
|
+
"files": [
|
|
12
|
+
"src",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://docs.tranzak.co"
|
|
19
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { FramedWidget, InlineWidget } from '@tranzakpackage/cards-core';
|
|
2
|
+
|
|
3
|
+
export class TranzakCardsError extends Error {
|
|
4
|
+
code: string;
|
|
5
|
+
status: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface BaseOptions {
|
|
9
|
+
apiKey: string;
|
|
10
|
+
/** Defaults to https://api.tranzak.co/api/cards/v1 */
|
|
11
|
+
baseUrl?: string;
|
|
12
|
+
fetchImpl?: typeof fetch;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function createWidgetSession(options: BaseOptions & {
|
|
16
|
+
cardId: string;
|
|
17
|
+
/** A website declared in your dashboard (Cards > Widget domains). */
|
|
18
|
+
origin: string;
|
|
19
|
+
layout?: 'inline' | 'framed';
|
|
20
|
+
permissions?: Array<'reveal' | 'freeze'>;
|
|
21
|
+
style?: { theme?: 'light' | 'dark'; radius?: 'none' | 'sm' | 'md' | 'lg'; font?: 'system' | 'serif' | 'mono' };
|
|
22
|
+
}): Promise<InlineWidget | FramedWidget>;
|
|
23
|
+
|
|
24
|
+
export function createNativeSession(options: BaseOptions & {
|
|
25
|
+
cardId: string;
|
|
26
|
+
permissions?: Array<'reveal' | 'freeze'>;
|
|
27
|
+
style?: { theme?: 'light' | 'dark'; radius?: 'none' | 'sm' | 'md' | 'lg'; font?: 'system' | 'serif' | 'mono' };
|
|
28
|
+
}): Promise<{ mode: 'native'; session_secret: string; expires_at: string; api_base: string }>;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tranzak Cards, côté SERVEUR (Node.js, routes Next.js, actions serveur). Votre clé API ne quitte jamais votre serveur.
|
|
3
|
+
*
|
|
4
|
+
* import { createWidgetSession } from '@tranzakpackage/cards-server';
|
|
5
|
+
* const widget = await createWidgetSession({ apiKey: process.env.TRANZAK_API_KEY, cardId, origin: 'https://shop.example.com' });
|
|
6
|
+
* // renvoyez `widget` à votre page : <TranzakCard widget={widget} />
|
|
7
|
+
*
|
|
8
|
+
* Vérifiez AVANT que l'utilisateur connecté est bien le propriétaire de cette carte.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const DEFAULT_BASE = 'https://api.tranzak.co/api/cards/v1';
|
|
12
|
+
|
|
13
|
+
export class TranzakCardsError extends Error {
|
|
14
|
+
constructor(code, status, message) {
|
|
15
|
+
super(message || code);
|
|
16
|
+
this.name = 'TranzakCardsError';
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.status = status;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Un identifiant de carte ne peut jamais fabriquer un autre chemin d'API. */
|
|
23
|
+
function assertCardId(cardId) {
|
|
24
|
+
if (typeof cardId !== 'string' || !/^card_[a-z0-9]{1,60}$/.test(cardId)) throw new TranzakCardsError('invalid_card_id', 0, 'invalid card id');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function request({ apiKey, baseUrl = DEFAULT_BASE, fetchImpl = globalThis.fetch }, method, path, body) {
|
|
28
|
+
if (!apiKey) throw new TranzakCardsError('api_key_missing', 0, 'apiKey is required');
|
|
29
|
+
let res;
|
|
30
|
+
try {
|
|
31
|
+
res = await fetchImpl(`${baseUrl}${path}`, {
|
|
32
|
+
method,
|
|
33
|
+
headers: { 'X-Api-Key': apiKey, Accept: 'application/json', 'Content-Type': 'application/json' },
|
|
34
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
35
|
+
cache: 'no-store',
|
|
36
|
+
});
|
|
37
|
+
} catch {
|
|
38
|
+
throw new TranzakCardsError('network', 0, 'Could not reach the Tranzak API');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let json = null;
|
|
42
|
+
try { json = await res.json(); } catch { /* corps non JSON */ }
|
|
43
|
+
if (!res.ok || !json || json.success !== true) {
|
|
44
|
+
throw new TranzakCardsError((json && json.error) || 'error', res.status, json && json.message);
|
|
45
|
+
}
|
|
46
|
+
return json;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Session web : renvoie l'objet `widget` à passer au composant. `layout: 'inline'` (par défaut) dessine la carte dans la
|
|
51
|
+
* page, avec les zones sensibles dans de petites iframes de Tranzak.
|
|
52
|
+
*/
|
|
53
|
+
export async function createWidgetSession({ cardId, origin, layout = 'inline', permissions, style, ...options }) {
|
|
54
|
+
assertCardId(cardId);
|
|
55
|
+
const json = await request(options, 'POST', `/cards/${cardId}/widget-sessions`, { origin, layout, permissions, style });
|
|
56
|
+
return json.widget;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Session native (application mobile) : renvoie { mode, session_secret, expires_at, api_base }. */
|
|
60
|
+
export async function createNativeSession({ cardId, permissions, style, ...options }) {
|
|
61
|
+
assertCardId(cardId);
|
|
62
|
+
const json = await request(options, 'POST', `/cards/${cardId}/widget-sessions`, { client: 'native', permissions, style });
|
|
63
|
+
return json.widget;
|
|
64
|
+
}
|