@mercuryo-ai/magicpay-sdk 0.1.0-test.2
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.md +56 -0
- package/README.md +426 -0
- package/dist/agentbrowse.d.ts +66 -0
- package/dist/agentbrowse.d.ts.map +1 -0
- package/dist/agentbrowse.js +174 -0
- package/dist/client.d.ts +50 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +61 -0
- package/dist/core.d.ts +7 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +3 -0
- package/dist/gateway.d.ts +67 -0
- package/dist/gateway.d.ts.map +1 -0
- package/dist/gateway.js +271 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/request-flow.d.ts +37 -0
- package/dist/request-flow.d.ts.map +1 -0
- package/dist/request-flow.js +70 -0
- package/dist/secret-flow.d.ts +228 -0
- package/dist/secret-flow.d.ts.map +1 -0
- package/dist/secret-flow.js +536 -0
- package/dist/session-client.d.ts +305 -0
- package/dist/session-client.d.ts.map +1 -0
- package/dist/session-client.js +82 -0
- package/dist/session-flow.d.ts +42 -0
- package/dist/session-flow.d.ts.map +1 -0
- package/dist/session-flow.js +71 -0
- package/docs/examples.md +432 -0
- package/package.json +94 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { resolveSecretCatalogContext, resolveCatalogHost } from './secret-flow.js';
|
|
2
|
+
function isStoredSecretKind(purpose) {
|
|
3
|
+
return purpose === 'login' || purpose === 'identity' || purpose === 'payment_card';
|
|
4
|
+
}
|
|
5
|
+
function candidateConfidence(formFieldKeys, candidateFieldKeys) {
|
|
6
|
+
const intersectionCount = candidateFieldKeys.filter((fieldKey) => formFieldKeys.has(fieldKey)).length;
|
|
7
|
+
if (intersectionCount === 0) {
|
|
8
|
+
return 'low';
|
|
9
|
+
}
|
|
10
|
+
if (intersectionCount === formFieldKeys.size) {
|
|
11
|
+
return 'high';
|
|
12
|
+
}
|
|
13
|
+
return 'medium';
|
|
14
|
+
}
|
|
15
|
+
function buildRequestFields(form, candidate) {
|
|
16
|
+
const allowedFieldKeys = candidate.fieldKeys && candidate.fieldKeys.length > 0 ? new Set(candidate.fieldKeys) : null;
|
|
17
|
+
return form.fields
|
|
18
|
+
.filter((field) => !allowedFieldKeys || allowedFieldKeys.has(field.fieldKey))
|
|
19
|
+
.map((field) => field.fieldKey);
|
|
20
|
+
}
|
|
21
|
+
function restrictObservedFormToCandidateFields(form, candidate) {
|
|
22
|
+
if (!candidate?.fieldKeys || candidate.fieldKeys.length === 0) {
|
|
23
|
+
return form;
|
|
24
|
+
}
|
|
25
|
+
const allowedFieldKeys = new Set(candidate.fieldKeys);
|
|
26
|
+
const filteredFields = form.fields.filter((field) => allowedFieldKeys.has(field.fieldKey));
|
|
27
|
+
if (filteredFields.length === 0 || filteredFields.length === form.fields.length) {
|
|
28
|
+
return filteredFields.length === 0 ? form : { ...form, fields: filteredFields };
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
...form,
|
|
32
|
+
fields: filteredFields,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function normalizeObservedFormHost(urlOrHost, catalog) {
|
|
36
|
+
if (typeof urlOrHost === 'string' && urlOrHost.trim().length > 0) {
|
|
37
|
+
try {
|
|
38
|
+
return {
|
|
39
|
+
host: resolveCatalogHost(urlOrHost),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return {
|
|
44
|
+
host: null,
|
|
45
|
+
reason: 'The provided page URL or host could not be resolved into a usable MagicPay catalog host.',
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (catalog?.host) {
|
|
50
|
+
return {
|
|
51
|
+
host: catalog.host,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
host: null,
|
|
56
|
+
reason: 'No page URL, host, or catalog host was available for secret request creation.',
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export function buildObservedFormStoredSecretCandidates(form, catalog) {
|
|
60
|
+
if (!catalog || !isStoredSecretKind(form.purpose)) {
|
|
61
|
+
return [];
|
|
62
|
+
}
|
|
63
|
+
const formFieldKeys = new Set(form.fields.map((field) => field.fieldKey));
|
|
64
|
+
return catalog.storedSecrets
|
|
65
|
+
.filter((secret) => secret.kind === form.purpose &&
|
|
66
|
+
secret.fieldKeys.some((fieldKey) => formFieldKeys.has(fieldKey)))
|
|
67
|
+
.map((secret) => ({
|
|
68
|
+
storedSecretRef: secret.storedSecretRef,
|
|
69
|
+
kind: secret.kind,
|
|
70
|
+
scope: secret.scope,
|
|
71
|
+
displayName: secret.displayName,
|
|
72
|
+
matchConfidence: candidateConfidence(formFieldKeys, secret.fieldKeys),
|
|
73
|
+
intentRequired: secret.intentRequired,
|
|
74
|
+
fieldKeys: [...secret.fieldKeys],
|
|
75
|
+
fieldPolicies: secret.fieldPolicies ? { ...secret.fieldPolicies } : undefined,
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
export function findObservedFormStoredSecretCandidate(form, catalog, storedSecretRef) {
|
|
79
|
+
if (!storedSecretRef) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return (buildObservedFormStoredSecretCandidates(form, catalog).find((candidate) => candidate.storedSecretRef === storedSecretRef) ?? null);
|
|
83
|
+
}
|
|
84
|
+
export function enrichObservedFormWithStoredSecrets(form, catalog) {
|
|
85
|
+
return {
|
|
86
|
+
...form,
|
|
87
|
+
storedSecretCandidates: buildObservedFormStoredSecretCandidates(form, catalog),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
export function enrichObservedFormsWithStoredSecrets(forms, catalog) {
|
|
91
|
+
return forms.map((form) => enrichObservedFormWithStoredSecrets(form, catalog));
|
|
92
|
+
}
|
|
93
|
+
export function enrichObservedFormsForUrl(forms, catalogByHost, urlOrHost) {
|
|
94
|
+
return enrichObservedFormsWithStoredSecrets(forms, resolveSecretCatalogContext(catalogByHost, urlOrHost).catalog);
|
|
95
|
+
}
|
|
96
|
+
export function buildRequestInputForObservedForm(params) {
|
|
97
|
+
const initialHost = normalizeObservedFormHost(params.urlOrHost, params.catalog ?? null);
|
|
98
|
+
if (!initialHost.host) {
|
|
99
|
+
return {
|
|
100
|
+
success: false,
|
|
101
|
+
kind: 'host_resolution_failed',
|
|
102
|
+
reason: initialHost.reason ??
|
|
103
|
+
'MagicPay could not determine a usable host for secret request creation.',
|
|
104
|
+
host: null,
|
|
105
|
+
catalog: params.catalog ?? null,
|
|
106
|
+
fillableForm: params.fillableForm,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const resolvedCatalog = params.catalog ?? null;
|
|
110
|
+
const candidate = findObservedFormStoredSecretCandidate(params.fillableForm, resolvedCatalog, params.storedSecretRef);
|
|
111
|
+
if (!resolvedCatalog) {
|
|
112
|
+
return {
|
|
113
|
+
success: false,
|
|
114
|
+
kind: 'stored_secret_not_available',
|
|
115
|
+
reason: 'No stored-secret catalog is available for the observed form and host.',
|
|
116
|
+
host: initialHost.host,
|
|
117
|
+
catalog: null,
|
|
118
|
+
fillableForm: params.fillableForm,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (!candidate) {
|
|
122
|
+
return {
|
|
123
|
+
success: false,
|
|
124
|
+
kind: 'stored_secret_not_available',
|
|
125
|
+
reason: 'The provided stored secret is not available for the observed form and host.',
|
|
126
|
+
host: initialHost.host,
|
|
127
|
+
catalog: resolvedCatalog,
|
|
128
|
+
fillableForm: params.fillableForm,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
const pageRef = params.page?.ref ?? params.fillableForm.pageRef;
|
|
132
|
+
return {
|
|
133
|
+
success: true,
|
|
134
|
+
host: initialHost.host,
|
|
135
|
+
catalog: resolvedCatalog,
|
|
136
|
+
candidate,
|
|
137
|
+
input: {
|
|
138
|
+
sessionId: params.sessionId,
|
|
139
|
+
clientRequestId: params.clientRequestId ?? `magicpay-sdk-request-${Date.now()}`,
|
|
140
|
+
fillRef: params.fillableForm.fillRef,
|
|
141
|
+
purpose: params.fillableForm.purpose,
|
|
142
|
+
merchantName: params.merchantName,
|
|
143
|
+
page: {
|
|
144
|
+
ref: pageRef,
|
|
145
|
+
...(params.page?.url ? { url: params.page.url } : {}),
|
|
146
|
+
...(params.page?.title ? { title: params.page.title } : {}),
|
|
147
|
+
},
|
|
148
|
+
secretHint: {
|
|
149
|
+
storedSecretRef: candidate.storedSecretRef,
|
|
150
|
+
credentialName: candidate.displayName,
|
|
151
|
+
kind: candidate.kind,
|
|
152
|
+
host: initialHost.host,
|
|
153
|
+
...(params.fillableForm.scopeRef ? { scopeRef: params.fillableForm.scopeRef } : {}),
|
|
154
|
+
fields: buildRequestFields(params.fillableForm, candidate),
|
|
155
|
+
},
|
|
156
|
+
...(params.run ? { run: params.run } : {}),
|
|
157
|
+
...(params.step ? { step: params.step } : {}),
|
|
158
|
+
},
|
|
159
|
+
fillableForm: params.fillableForm,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
export function prepareProtectedFillFromClaim(params) {
|
|
163
|
+
const storedSecretRef = params.claim.secret.storedSecretRef ?? params.request?.storedSecretRef ?? null;
|
|
164
|
+
const storedSecretCandidate = findObservedFormStoredSecretCandidate(params.fillableForm, params.catalog, storedSecretRef ?? undefined);
|
|
165
|
+
return {
|
|
166
|
+
fillableForm: restrictObservedFormToCandidateFields(params.fillableForm, storedSecretCandidate ?? undefined),
|
|
167
|
+
storedSecretCandidate,
|
|
168
|
+
protectedValues: { ...params.claim.secret.values },
|
|
169
|
+
storedSecretRef,
|
|
170
|
+
...(storedSecretCandidate?.fieldPolicies
|
|
171
|
+
? { fieldPolicies: storedSecretCandidate.fieldPolicies }
|
|
172
|
+
: {}),
|
|
173
|
+
};
|
|
174
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { MagicPayGatewayConfig } from './gateway.js';
|
|
2
|
+
import { type CompleteRemoteSessionOutcome } from './session-flow.js';
|
|
3
|
+
import { type CompleteRemoteSessionInput, type CreateRemoteSessionInput, type SessionResponse } from './session-client.js';
|
|
4
|
+
import { type ClaimSecretRequestOutcome, type CreateSecretRequestInput, type CreateSecretRequestResult, type SecretCatalog, type SecretRequestPollOutcome, type SecretRequestPollUntilOptions, type SecretRequestPollUntilOutcome } from './secret-flow.js';
|
|
5
|
+
import type { SecretRequestPollAttempt } from './secret-flow.js';
|
|
6
|
+
import { type RemoteSessionState } from './request-flow.js';
|
|
7
|
+
/**
|
|
8
|
+
* Top-level client configuration for the consumer-facing MagicPay SDK.
|
|
9
|
+
*/
|
|
10
|
+
export interface MagicPayClientOptions {
|
|
11
|
+
gateway: MagicPayGatewayConfig;
|
|
12
|
+
fetchImpl?: typeof fetch;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Per-request transport options for high-level client calls.
|
|
16
|
+
*/
|
|
17
|
+
export interface MagicPayClientRequestOptions {
|
|
18
|
+
signal?: AbortSignal;
|
|
19
|
+
timeoutMs?: number;
|
|
20
|
+
}
|
|
21
|
+
export interface MagicPayPollUntilOptions extends SecretRequestPollUntilOptions {
|
|
22
|
+
onAttempt?: (attempt: SecretRequestPollAttempt) => void;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* High-level MagicPay client.
|
|
26
|
+
*
|
|
27
|
+
* The root client owns networked session and secret-request flows.
|
|
28
|
+
* Domain helpers live in `@mercuryo-ai/magicpay-sdk/core`.
|
|
29
|
+
* AgentBrowse bridge helpers live in `@mercuryo-ai/magicpay-sdk/agentbrowse`.
|
|
30
|
+
*/
|
|
31
|
+
export interface MagicPayClient {
|
|
32
|
+
readonly gateway: MagicPayGatewayConfig;
|
|
33
|
+
readonly sessions: {
|
|
34
|
+
create(input: CreateRemoteSessionInput, options?: MagicPayClientRequestOptions): Promise<SessionResponse>;
|
|
35
|
+
get(sessionId: string, options?: MagicPayClientRequestOptions): Promise<SessionResponse>;
|
|
36
|
+
getState(sessionId: string, options?: MagicPayClientRequestOptions): Promise<RemoteSessionState>;
|
|
37
|
+
describe(session: SessionResponse): RemoteSessionState;
|
|
38
|
+
completeWithOutcome(sessionId: string, input: CompleteRemoteSessionInput, options?: MagicPayClientRequestOptions): Promise<CompleteRemoteSessionOutcome>;
|
|
39
|
+
};
|
|
40
|
+
readonly secrets: {
|
|
41
|
+
fetchCatalog(sessionId: string, urlOrHost: string, options?: MagicPayClientRequestOptions): Promise<SecretCatalog>;
|
|
42
|
+
createRequest(input: CreateSecretRequestInput, options?: MagicPayClientRequestOptions): Promise<CreateSecretRequestResult>;
|
|
43
|
+
poll(sessionId: string, requestId: string, options?: MagicPayClientRequestOptions): Promise<SecretRequestPollOutcome>;
|
|
44
|
+
pollUntil(sessionId: string, requestId: string, options?: MagicPayPollUntilOptions): Promise<SecretRequestPollUntilOutcome>;
|
|
45
|
+
claim(sessionId: string, requestId: string, options?: MagicPayClientRequestOptions): Promise<ClaimSecretRequestOutcome>;
|
|
46
|
+
claim(sessionId: string, requestId: string, claimId: string, options?: MagicPayClientRequestOptions): Promise<ClaimSecretRequestOutcome>;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export declare function createMagicPayClient(options: MagicPayClientOptions): MagicPayClient;
|
|
50
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAA0B,MAAM,cAAc,CAAC;AAClF,OAAO,EAEL,KAAK,4BAA4B,EAClC,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAGL,KAAK,0BAA0B,EAC/B,KAAK,wBAAwB,EAC7B,KAAK,eAAe,EACrB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAML,KAAK,yBAAyB,EAC9B,KAAK,wBAAwB,EA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CAAC;KAC1C,CAAC;IACF,QAAQ,CAAC,OAAO,EAAE;QAChB,YAAY,CACV,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,4BAA4B,GACrC,OAAO,CAAC,aAAa,CAAC,CAAC;QAC1B,aAAa,CACX,KAAK,EAAE,wBAAwB,EAC/B,OAAO,CAAC,EAAE,4BAA4B,GACrC,OAAO,CAAC,yBAAyB,CAAC,CAAC;QACtC,IAAI,CACF,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,4BAA4B,GACrC,OAAO,CAAC,wBAAwB,CAAC,CAAC;QACrC,SAAS,CACP,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,wBAAwB,GACjC,OAAO,CAAC,6BAA6B,CAAC,CAAC;QAC1C,KAAK,CACH,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,4BAA4B,GACrC,OAAO,CAAC,yBAAyB,CAAC,CAAC;QACtC,KAAK,CACH,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,4BAA4B,GACrC,OAAO,CAAC,yBAAyB,CAAC,CAAC;KACvC,CAAC;CACH;AAiBD,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,qBAAqB,GAAG,cAAc,CAgGnF"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { completeRemoteSessionWithOutcome, } from './session-flow.js';
|
|
2
|
+
import { createRemoteSession, getRemoteSession, } from './session-client.js';
|
|
3
|
+
import { claimSecretRequest, createSecretRequest, fetchSecretCatalog, pollSecretRequest, pollSecretRequestUntil, } from './secret-flow.js';
|
|
4
|
+
import { describeRemoteSessionState } from './request-flow.js';
|
|
5
|
+
function toRequestOptions(fetchImpl, options) {
|
|
6
|
+
return {
|
|
7
|
+
...(fetchImpl ? { fetchImpl } : {}),
|
|
8
|
+
...(options?.signal ? { signal: options.signal } : {}),
|
|
9
|
+
...(options?.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function buildClaimId() {
|
|
13
|
+
return `magicpay-sdk-claim-${Date.now()}`;
|
|
14
|
+
}
|
|
15
|
+
export function createMagicPayClient(options) {
|
|
16
|
+
const { gateway, fetchImpl } = options;
|
|
17
|
+
return {
|
|
18
|
+
gateway,
|
|
19
|
+
sessions: {
|
|
20
|
+
async create(input, request) {
|
|
21
|
+
return createRemoteSession(gateway, input, toRequestOptions(fetchImpl, request));
|
|
22
|
+
},
|
|
23
|
+
async get(sessionId, request) {
|
|
24
|
+
return getRemoteSession(gateway, sessionId, toRequestOptions(fetchImpl, request));
|
|
25
|
+
},
|
|
26
|
+
async getState(sessionId, request) {
|
|
27
|
+
return describeRemoteSessionState(await getRemoteSession(gateway, sessionId, toRequestOptions(fetchImpl, request)));
|
|
28
|
+
},
|
|
29
|
+
describe(session) {
|
|
30
|
+
return describeRemoteSessionState(session);
|
|
31
|
+
},
|
|
32
|
+
async completeWithOutcome(sessionId, input, request) {
|
|
33
|
+
return completeRemoteSessionWithOutcome(gateway, sessionId, input, toRequestOptions(fetchImpl, request));
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
secrets: {
|
|
37
|
+
async fetchCatalog(sessionId, urlOrHost, request) {
|
|
38
|
+
return fetchSecretCatalog(gateway, sessionId, urlOrHost, toRequestOptions(fetchImpl, request));
|
|
39
|
+
},
|
|
40
|
+
async createRequest(input, request) {
|
|
41
|
+
return createSecretRequest(gateway, input, toRequestOptions(fetchImpl, request));
|
|
42
|
+
},
|
|
43
|
+
async poll(sessionId, requestId, request) {
|
|
44
|
+
return pollSecretRequest(gateway, sessionId, requestId, toRequestOptions(fetchImpl, request));
|
|
45
|
+
},
|
|
46
|
+
async pollUntil(sessionId, requestId, pollOptions) {
|
|
47
|
+
return pollSecretRequestUntil(gateway, sessionId, requestId, {
|
|
48
|
+
...(fetchImpl ? { fetchImpl } : {}),
|
|
49
|
+
...(pollOptions ?? {}),
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
async claim(sessionId, requestId, claimIdOrOptions, maybeOptions) {
|
|
53
|
+
const claimId = typeof claimIdOrOptions === 'string'
|
|
54
|
+
? claimIdOrOptions
|
|
55
|
+
: buildClaimId();
|
|
56
|
+
const request = typeof claimIdOrOptions === 'string' ? maybeOptions : claimIdOrOptions;
|
|
57
|
+
return claimSecretRequest(gateway, sessionId, requestId, claimId, toRequestOptions(fetchImpl, request));
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { classifySessionRequestDomain, describeRemoteSessionState, describeSessionRequestState, isApprovalRequestType, isPaymentFlowRequestType, isSessionRequestTerminal, } from './request-flow.js';
|
|
2
|
+
export type { RemoteSessionState, SessionPhase, SessionRequestDomain, SessionRequestLike, SessionRequestState, SessionRequestStatus, SessionRequestType, } from './request-flow.js';
|
|
3
|
+
export { buildCompleteRemoteSessionInput, buildCreateRemoteSessionInput, classifyCompleteRemoteSessionFailure, completeRemoteSessionWithOutcome, } from './session-flow.js';
|
|
4
|
+
export type { CompleteRemoteSessionFailureKind, CompleteRemoteSessionOutcome, RemoteSessionPageSummary, } from './session-flow.js';
|
|
5
|
+
export { claimSecretRequest, createSecretRequest, describeSecretRequestStatus, evaluateSecretRequestForFill, fetchSecretCatalog, pollSecretRequest, pollSecretRequestUntil, resolveSecretCatalogContext, resolveCatalogHost, } from './secret-flow.js';
|
|
6
|
+
export type { ClaimSecretRequestOutcome, ClaimSecretRequestResult, CreateSecretRequestInput, CreateSecretRequestResult, ProtectedFieldPolicy, SecretCatalog, SecretCatalogByHost, SecretClaimFailureKind, SecretRequestPollAttempt, SecretRequestContextMismatchReason, SecretRequestFillReadiness, SecretRequestHint, SecretRequestHintField, SecretRequestPollFailureKind, SecretRequestPollOutcome, SecretRequestPollResult, SecretRequestPollUntilFailureKind, SecretRequestPollUntilOptions, SecretRequestPollUntilOutcome, SecretRequestSession, SecretRequestSnapshot, SecretRequestStatus, SecretRequestStatusContract, SecretRequestType, StoredSecretApplicability, StoredSecretApplicabilityTarget, StoredSecretFieldKey, StoredSecretFieldPolicies, StoredSecretKind, StoredSecretMetadata, StoredSecretScope, } from './secret-flow.js';
|
|
7
|
+
//# sourceMappingURL=core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,4BAA4B,EAC5B,0BAA0B,EAC1B,2BAA2B,EAC3B,qBAAqB,EACrB,wBAAwB,EACxB,wBAAwB,GACzB,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EACV,kBAAkB,EAClB,YAAY,EACZ,oBAAoB,EACpB,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,kBAAkB,GACnB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACL,+BAA+B,EAC/B,6BAA6B,EAC7B,oCAAoC,EACpC,gCAAgC,GACjC,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EACV,gCAAgC,EAChC,4BAA4B,EAC5B,wBAAwB,GACzB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,2BAA2B,EAC3B,4BAA4B,EAC5B,kBAAkB,EAClB,iBAAiB,EACjB,sBAAsB,EACtB,2BAA2B,EAC3B,kBAAkB,GACnB,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EACV,yBAAyB,EACzB,wBAAwB,EACxB,wBAAwB,EACxB,yBAAyB,EACzB,oBAAoB,EACpB,aAAa,EACb,mBAAmB,EACnB,sBAAsB,EACtB,wBAAwB,EACxB,kCAAkC,EAClC,0BAA0B,EAC1B,iBAAiB,EACjB,sBAAsB,EACtB,4BAA4B,EAC5B,wBAAwB,EACxB,uBAAuB,EACvB,iCAAiC,EACjC,6BAA6B,EAC7B,6BAA6B,EAC7B,oBAAoB,EACpB,qBAAqB,EACrB,mBAAmB,EACnB,2BAA2B,EAC3B,iBAAiB,EACjB,yBAAyB,EACzB,+BAA+B,EAC/B,oBAAoB,EACpB,yBAAyB,EACzB,gBAAgB,EAChB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC"}
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { classifySessionRequestDomain, describeRemoteSessionState, describeSessionRequestState, isApprovalRequestType, isPaymentFlowRequestType, isSessionRequestTerminal, } from './request-flow.js';
|
|
2
|
+
export { buildCompleteRemoteSessionInput, buildCreateRemoteSessionInput, classifyCompleteRemoteSessionFailure, completeRemoteSessionWithOutcome, } from './session-flow.js';
|
|
3
|
+
export { claimSecretRequest, createSecretRequest, describeSecretRequestStatus, evaluateSecretRequestForFill, fetchSecretCatalog, pollSecretRequest, pollSecretRequestUntil, resolveSecretCatalogContext, resolveCatalogHost, } from './secret-flow.js';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connection settings for a trusted MagicPay caller.
|
|
3
|
+
*
|
|
4
|
+
* Use backend-owned credentials here. The SDK is not meant to expose this
|
|
5
|
+
* configuration directly to an untrusted browser runtime.
|
|
6
|
+
*/
|
|
7
|
+
export interface MagicPayGatewayConfig {
|
|
8
|
+
apiKey: string;
|
|
9
|
+
apiUrl: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Request-scoped transport options for low-level MagicPay API calls.
|
|
13
|
+
*/
|
|
14
|
+
export interface MagicPayRequestOptions {
|
|
15
|
+
fetchImpl?: typeof fetch;
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
timeoutMs?: number;
|
|
18
|
+
}
|
|
19
|
+
export interface AgentBackendStats {
|
|
20
|
+
monthly_spend: number;
|
|
21
|
+
intent_count_month: number;
|
|
22
|
+
active_subscriptions: number;
|
|
23
|
+
avg_risk_score: number;
|
|
24
|
+
}
|
|
25
|
+
export interface AgentBackendProfile {
|
|
26
|
+
id: string;
|
|
27
|
+
name: string;
|
|
28
|
+
description: string | null;
|
|
29
|
+
status: string;
|
|
30
|
+
budget_limit: number | null;
|
|
31
|
+
daily_limit: number | null;
|
|
32
|
+
transaction_limit: number | null;
|
|
33
|
+
created_at: string;
|
|
34
|
+
stats: AgentBackendStats | null;
|
|
35
|
+
}
|
|
36
|
+
export declare class MagicPayRequestError extends Error {
|
|
37
|
+
readonly status: number;
|
|
38
|
+
readonly payload: unknown;
|
|
39
|
+
readonly errorCode: string | null;
|
|
40
|
+
readonly responseText: string;
|
|
41
|
+
readonly contentType: string | null;
|
|
42
|
+
constructor(message: string, options: {
|
|
43
|
+
status: number;
|
|
44
|
+
payload: unknown;
|
|
45
|
+
errorCode?: string | null;
|
|
46
|
+
responseText?: string;
|
|
47
|
+
contentType?: string | null;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
export declare function isMagicPayAbortError(error: unknown): boolean;
|
|
51
|
+
export declare function getMagicPayErrorCode(payload: unknown): string | null;
|
|
52
|
+
export declare function getMagicPayErrorMessage(payload: unknown): string | null;
|
|
53
|
+
export declare function isMagicPayRequestErrorStatus(error: unknown, status: number): boolean;
|
|
54
|
+
export declare function matchesMagicPayRequestErrorMessage(error: unknown, expected: string | readonly string[]): boolean;
|
|
55
|
+
export declare function requestMagicPayJson<TResponse>(gateway: MagicPayGatewayConfig, method: 'GET' | 'POST', url: string, body?: unknown, options?: MagicPayRequestOptions): Promise<TResponse>;
|
|
56
|
+
export declare function normalizeMagicPayGatewayConfig(gateway: MagicPayGatewayConfig): MagicPayGatewayConfig;
|
|
57
|
+
export declare function buildMagicPayAgentMeUrl(apiUrl: string): string;
|
|
58
|
+
export declare function buildMagicPaySessionsUrl(apiUrl: string): string;
|
|
59
|
+
export declare function buildMagicPaySessionUrl(apiUrl: string, sessionId: string): string;
|
|
60
|
+
export declare function buildMagicPaySessionSecretsCatalogUrl(apiUrl: string, sessionId: string): string;
|
|
61
|
+
export declare function buildMagicPaySessionEventsUrl(apiUrl: string, sessionId: string): string;
|
|
62
|
+
export declare function buildMagicPayCompleteSessionUrl(apiUrl: string, sessionId: string): string;
|
|
63
|
+
export declare function buildMagicPaySessionSecretRequestsUrl(apiUrl: string, sessionId: string): string;
|
|
64
|
+
export declare function buildMagicPaySessionSecretRequestUrl(apiUrl: string, sessionId: string, requestId: string): string;
|
|
65
|
+
export declare function buildMagicPayClaimSecretUrl(apiUrl: string, sessionId: string, requestId: string): string;
|
|
66
|
+
export declare function getAuthenticatedAgent(gateway: MagicPayGatewayConfig, options?: MagicPayRequestOptions): Promise<AgentBackendProfile>;
|
|
67
|
+
//# sourceMappingURL=gateway.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway.d.ts","sourceRoot":"","sources":["../src/gateway.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,SAAS,CAAC,EAAE,OAAO,KAAK,CAAC;IACzB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,iBAAiB;IAChC,aAAa,EAAE,MAAM,CAAC;IACtB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,oBAAoB,EAAE,MAAM,CAAC;IAC7B,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,mBAAmB;IAClC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC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RequestError extends Error {
|
|
2
|
+
status;
|
|
3
|
+
payload;
|
|
4
|
+
errorCode;
|
|
5
|
+
responseText;
|
|
6
|
+
contentType;
|
|
7
|
+
constructor(message, options) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = 'MagicPayRequestError';
|
|
10
|
+
this.status = options.status;
|
|
11
|
+
this.payload = options.payload;
|
|
12
|
+
this.errorCode = options.errorCode ?? null;
|
|
13
|
+
this.responseText = options.responseText ?? '';
|
|
14
|
+
this.contentType = options.contentType ?? null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export function isMagicPayAbortError(error) {
|
|
18
|
+
return error instanceof Error && error.name === 'AbortError';
|
|
19
|
+
}
|
|
20
|
+
function normalizeApiUrl(value) {
|
|
21
|
+
return value.replace(/\/$/, '');
|
|
22
|
+
}
|
|
23
|
+
function appendApiPath(apiUrl, suffix) {
|
|
24
|
+
return `${normalizeApiUrl(apiUrl)}${suffix.startsWith('/') ? suffix : `/${suffix}`}`;
|
|
25
|
+
}
|
|
26
|
+
function asJsonRecord(value) {
|
|
27
|
+
return value && typeof value === 'object' && !Array.isArray(value)
|
|
28
|
+
? value
|
|
29
|
+
: null;
|
|
30
|
+
}
|
|
31
|
+
function getHeaderValue(headers, name) {
|
|
32
|
+
return typeof headers?.get === 'function' ? headers.get(name) : null;
|
|
33
|
+
}
|
|
34
|
+
function parseJsonResponseBody(rawText) {
|
|
35
|
+
if (rawText.length === 0) {
|
|
36
|
+
return { payload: null, parseError: null };
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
return {
|
|
40
|
+
payload: JSON.parse(rawText),
|
|
41
|
+
parseError: null,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
if (error instanceof SyntaxError) {
|
|
46
|
+
return {
|
|
47
|
+
payload: null,
|
|
48
|
+
parseError: error,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export function getMagicPayErrorCode(payload) {
|
|
55
|
+
if (payload instanceof MagicPayRequestError) {
|
|
56
|
+
if (payload.errorCode) {
|
|
57
|
+
return payload.errorCode;
|
|
58
|
+
}
|
|
59
|
+
return getMagicPayErrorCode(payload.payload);
|
|
60
|
+
}
|
|
61
|
+
const record = asJsonRecord(payload);
|
|
62
|
+
if (!record) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
if (typeof record.error_code === 'string' && record.error_code.trim().length > 0) {
|
|
66
|
+
return record.error_code.trim();
|
|
67
|
+
}
|
|
68
|
+
if (typeof record.code === 'string' && record.code.trim().length > 0) {
|
|
69
|
+
return record.code.trim();
|
|
70
|
+
}
|
|
71
|
+
return deriveFallbackMagicPayErrorCode(getMagicPayErrorMessage(record));
|
|
72
|
+
}
|
|
73
|
+
export function getMagicPayErrorMessage(payload) {
|
|
74
|
+
if (payload instanceof MagicPayRequestError) {
|
|
75
|
+
const payloadMessage = getMagicPayErrorMessage(payload.payload);
|
|
76
|
+
if (payloadMessage) {
|
|
77
|
+
return payloadMessage;
|
|
78
|
+
}
|
|
79
|
+
return payload.message;
|
|
80
|
+
}
|
|
81
|
+
const record = asJsonRecord(payload);
|
|
82
|
+
if (!record) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
if (typeof record.error === 'string' && record.error.trim().length > 0) {
|
|
86
|
+
return record.error.trim();
|
|
87
|
+
}
|
|
88
|
+
if (typeof record.message === 'string' && record.message.trim().length > 0) {
|
|
89
|
+
return record.message.trim();
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
function normalizeErrorMessageForMatch(value) {
|
|
94
|
+
return value.trim().replace(/[.!?]+$/, '').toLowerCase();
|
|
95
|
+
}
|
|
96
|
+
const FALLBACK_ERROR_CODES_BY_MESSAGE = new Map([
|
|
97
|
+
['this workflow session is already closed', 'session_closed'],
|
|
98
|
+
['workflow session is already closed', 'session_closed'],
|
|
99
|
+
['this secret request is not approved yet', 'secret_request_not_fulfilled'],
|
|
100
|
+
['this secret request does not have usable secret values', 'secret_request_not_fulfilled'],
|
|
101
|
+
[
|
|
102
|
+
'this secret request does not have resolved secret values yet',
|
|
103
|
+
'secret_request_not_fulfilled',
|
|
104
|
+
],
|
|
105
|
+
['secret_request_not_fulfilled', 'secret_request_not_fulfilled'],
|
|
106
|
+
['a secret payload was already claimed for this request', 'secret_request_already_claimed'],
|
|
107
|
+
['this secret request is already claimed', 'secret_request_already_claimed'],
|
|
108
|
+
['secret_request_already_claimed', 'secret_request_already_claimed'],
|
|
109
|
+
]);
|
|
110
|
+
function deriveFallbackMagicPayErrorCode(message) {
|
|
111
|
+
if (!message) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
return FALLBACK_ERROR_CODES_BY_MESSAGE.get(normalizeErrorMessageForMatch(message)) ?? null;
|
|
115
|
+
}
|
|
116
|
+
export function isMagicPayRequestErrorStatus(error, status) {
|
|
117
|
+
return error instanceof MagicPayRequestError && error.status === status;
|
|
118
|
+
}
|
|
119
|
+
export function matchesMagicPayRequestErrorMessage(error, expected) {
|
|
120
|
+
const actualMessage = getMagicPayErrorMessage(error);
|
|
121
|
+
if (!actualMessage) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
const normalizedActual = normalizeErrorMessageForMatch(actualMessage);
|
|
125
|
+
const expectedMessages = Array.isArray(expected) ? expected : [expected];
|
|
126
|
+
return expectedMessages.some((candidate) => normalizeErrorMessageForMatch(candidate) === normalizedActual);
|
|
127
|
+
}
|
|
128
|
+
function buildMagicPayRequestError(response, payload, rawText) {
|
|
129
|
+
return new MagicPayRequestError(getMagicPayErrorMessage(payload) ?? `MagicPay API request failed with HTTP ${response.status}`, {
|
|
130
|
+
status: response.status,
|
|
131
|
+
payload,
|
|
132
|
+
errorCode: getMagicPayErrorCode(payload),
|
|
133
|
+
responseText: rawText,
|
|
134
|
+
contentType: getHeaderValue(response.headers, 'content-type'),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
function buildMagicPayResponseParseError(response, rawText) {
|
|
138
|
+
return new MagicPayRequestError(`MagicPay API returned a non-JSON response for HTTP ${response.status}`, {
|
|
139
|
+
status: response.status,
|
|
140
|
+
payload: null,
|
|
141
|
+
responseText: rawText,
|
|
142
|
+
contentType: getHeaderValue(response.headers, 'content-type'),
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
function buildMagicPayTimeoutError(timeoutMs) {
|
|
146
|
+
return new MagicPayRequestError(`MagicPay API request timed out after ${timeoutMs}ms`, {
|
|
147
|
+
status: 0,
|
|
148
|
+
payload: null,
|
|
149
|
+
errorCode: 'request_timeout',
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
function createRequestSignal(sourceSignal, timeoutMs) {
|
|
153
|
+
if (timeoutMs !== undefined && (!Number.isFinite(timeoutMs) || timeoutMs < 0)) {
|
|
154
|
+
throw new RangeError('MagicPay request timeout must be a finite non-negative number.');
|
|
155
|
+
}
|
|
156
|
+
if (!sourceSignal && timeoutMs === undefined) {
|
|
157
|
+
return {
|
|
158
|
+
signal: undefined,
|
|
159
|
+
cleanup: () => { },
|
|
160
|
+
didTimeout: () => false,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
const controller = new AbortController();
|
|
164
|
+
let didTimeout = false;
|
|
165
|
+
let timeoutId;
|
|
166
|
+
const abortFromSource = () => {
|
|
167
|
+
controller.abort(sourceSignal?.reason);
|
|
168
|
+
};
|
|
169
|
+
if (sourceSignal) {
|
|
170
|
+
if (sourceSignal.aborted) {
|
|
171
|
+
controller.abort(sourceSignal.reason);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
sourceSignal.addEventListener('abort', abortFromSource, { once: true });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (timeoutMs !== undefined) {
|
|
178
|
+
timeoutId = setTimeout(() => {
|
|
179
|
+
didTimeout = true;
|
|
180
|
+
controller.abort(buildMagicPayTimeoutError(timeoutMs));
|
|
181
|
+
}, timeoutMs);
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
signal: controller.signal,
|
|
185
|
+
cleanup: () => {
|
|
186
|
+
if (timeoutId) {
|
|
187
|
+
clearTimeout(timeoutId);
|
|
188
|
+
}
|
|
189
|
+
if (sourceSignal) {
|
|
190
|
+
sourceSignal.removeEventListener('abort', abortFromSource);
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
didTimeout: () => didTimeout,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
export async function requestMagicPayJson(gateway, method, url, body, options = {}) {
|
|
197
|
+
const normalizedGateway = normalizeMagicPayGatewayConfig(gateway);
|
|
198
|
+
const { signal, cleanup, didTimeout } = createRequestSignal(options.signal, options.timeoutMs);
|
|
199
|
+
try {
|
|
200
|
+
const response = await (options.fetchImpl ?? fetch)(url, {
|
|
201
|
+
method,
|
|
202
|
+
headers: {
|
|
203
|
+
authorization: `Bearer ${normalizedGateway.apiKey}`,
|
|
204
|
+
...(body === undefined ? {} : { 'content-type': 'application/json' }),
|
|
205
|
+
},
|
|
206
|
+
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
207
|
+
...(signal ? { signal } : {}),
|
|
208
|
+
});
|
|
209
|
+
const rawText = await response.text();
|
|
210
|
+
const { payload, parseError } = parseJsonResponseBody(rawText);
|
|
211
|
+
if (parseError) {
|
|
212
|
+
throw buildMagicPayResponseParseError(response, rawText);
|
|
213
|
+
}
|
|
214
|
+
if (!response.ok) {
|
|
215
|
+
throw buildMagicPayRequestError(response, payload, rawText);
|
|
216
|
+
}
|
|
217
|
+
return payload;
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
if (didTimeout() && isMagicPayAbortError(error)) {
|
|
221
|
+
throw buildMagicPayTimeoutError(options.timeoutMs ?? 0);
|
|
222
|
+
}
|
|
223
|
+
if (error instanceof MagicPayRequestError) {
|
|
224
|
+
throw error;
|
|
225
|
+
}
|
|
226
|
+
if (signal?.aborted && signal.reason instanceof MagicPayRequestError) {
|
|
227
|
+
throw signal.reason;
|
|
228
|
+
}
|
|
229
|
+
throw error;
|
|
230
|
+
}
|
|
231
|
+
finally {
|
|
232
|
+
cleanup();
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
export function normalizeMagicPayGatewayConfig(gateway) {
|
|
236
|
+
return {
|
|
237
|
+
apiKey: gateway.apiKey,
|
|
238
|
+
apiUrl: normalizeApiUrl(gateway.apiUrl),
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
export function buildMagicPayAgentMeUrl(apiUrl) {
|
|
242
|
+
return appendApiPath(apiUrl, '/agent/me');
|
|
243
|
+
}
|
|
244
|
+
export function buildMagicPaySessionsUrl(apiUrl) {
|
|
245
|
+
return appendApiPath(apiUrl, '/sessions');
|
|
246
|
+
}
|
|
247
|
+
export function buildMagicPaySessionUrl(apiUrl, sessionId) {
|
|
248
|
+
return appendApiPath(apiUrl, `/sessions/${sessionId}`);
|
|
249
|
+
}
|
|
250
|
+
export function buildMagicPaySessionSecretsCatalogUrl(apiUrl, sessionId) {
|
|
251
|
+
return appendApiPath(apiUrl, `/sessions/${sessionId}/secrets/catalog`);
|
|
252
|
+
}
|
|
253
|
+
export function buildMagicPaySessionEventsUrl(apiUrl, sessionId) {
|
|
254
|
+
return appendApiPath(apiUrl, `/sessions/${sessionId}/events`);
|
|
255
|
+
}
|
|
256
|
+
export function buildMagicPayCompleteSessionUrl(apiUrl, sessionId) {
|
|
257
|
+
return appendApiPath(apiUrl, `/sessions/${sessionId}/complete`);
|
|
258
|
+
}
|
|
259
|
+
export function buildMagicPaySessionSecretRequestsUrl(apiUrl, sessionId) {
|
|
260
|
+
return appendApiPath(apiUrl, `/sessions/${sessionId}/secret-requests`);
|
|
261
|
+
}
|
|
262
|
+
export function buildMagicPaySessionSecretRequestUrl(apiUrl, sessionId, requestId) {
|
|
263
|
+
return appendApiPath(apiUrl, `/sessions/${sessionId}/secret-requests/${requestId}`);
|
|
264
|
+
}
|
|
265
|
+
export function buildMagicPayClaimSecretUrl(apiUrl, sessionId, requestId) {
|
|
266
|
+
return appendApiPath(apiUrl, `/sessions/${sessionId}/secret-requests/${requestId}/claim-secret`);
|
|
267
|
+
}
|
|
268
|
+
export async function getAuthenticatedAgent(gateway, options = {}) {
|
|
269
|
+
const normalizedGateway = normalizeMagicPayGatewayConfig(gateway);
|
|
270
|
+
return requestMagicPayJson(normalizedGateway, 'GET', buildMagicPayAgentMeUrl(normalizedGateway.apiUrl), undefined, options);
|
|
271
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { MagicPayRequestError, getMagicPayErrorCode, getMagicPayErrorMessage, isMagicPayRequestErrorStatus, } from './gateway.js';
|
|
2
|
+
export type { MagicPayGatewayConfig } from './gateway.js';
|
|
3
|
+
export { createMagicPayClient } from './client.js';
|
|
4
|
+
export type { MagicPayClient, MagicPayClientRequestOptions, MagicPayClientOptions, MagicPayPollUntilOptions, } from './client.js';
|
|
5
|
+
export type { RemoteSessionState, SessionPhase, SessionRequestDomain, SessionRequestLike, SessionRequestState, SessionRequestStatus, SessionRequestType, } from './request-flow.js';
|
|
6
|
+
export type { CompleteRemoteSessionFailureKind, CompleteRemoteSessionOutcome, RemoteSessionPageSummary, } from './session-flow.js';
|
|
7
|
+
export type { CompleteRemoteSessionInput, CompleteRemoteSessionResponse, CreateRemoteSessionInput, SessionResponse, } from './session-client.js';
|
|
8
|
+
export type { CreateSecretRequestInput, CreateSecretRequestResult, ClaimSecretRequestOutcome, ClaimSecretRequestResult, ProtectedFieldPolicy, SecretCatalog, SecretCatalogByHost, SecretClaimFailureKind, SecretRequestPollAttempt, SecretRequestHint, SecretRequestHintField, SecretRequestPollFailureKind, SecretRequestPollOutcome, SecretRequestPollResult, SecretRequestPollUntilFailureKind, SecretRequestPollUntilOptions, SecretRequestPollUntilOutcome, SecretRequestSession, SecretRequestSnapshot, SecretRequestStatus, SecretRequestStatusContract, SecretRequestType, StoredSecretApplicability, StoredSecretApplicabilityTarget, StoredSecretFieldKey, StoredSecretFieldPolicies, StoredSecretKind, StoredSecretMetadata, StoredSecretScope, } from './secret-flow.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|