@geekapps/post-nextjs 0.1.1 → 0.1.3
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/dist/geekapps-post.d.ts +11 -0
- package/dist/geekapps-post.d.ts.map +1 -0
- package/dist/geekapps-post.js +76 -0
- package/dist/geekapps-post.js.map +1 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -2
- package/dist/index.js.map +1 -1
- package/dist/server-exports.d.ts +2 -0
- package/dist/server-exports.d.ts.map +1 -1
- package/dist/server-exports.js +1 -0
- package/dist/server-exports.js.map +1 -1
- package/package.json +1 -9
- package/src/{service-account.ts → geekapps-post.ts} +23 -20
- package/src/index.ts +2 -4
- package/src/react-exports.ts +0 -4
- package/src/react.ts +0 -38
- package/src/server-exports.ts +0 -8
- package/src/server.ts +0 -42
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { PostClient } from "./client.js";
|
|
2
|
+
export interface GeekappsPostOptions {
|
|
3
|
+
issuerUrl: string;
|
|
4
|
+
clientId: string;
|
|
5
|
+
clientSecret: string;
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class GeekappsPost extends PostClient {
|
|
9
|
+
constructor(options: GeekappsPostOptions);
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=geekapps-post.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geekapps-post.d.ts","sourceRoot":"","sources":["../src/geekapps-post.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,MAAM,WAAW,mBAAmB;IAClC,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAiFD,qBAAa,YAAa,SAAQ,UAAU;gBAC9B,OAAO,EAAE,mBAAmB;CAUzC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { PostClient } from "./client.js";
|
|
2
|
+
function decodeJwtPayload(token) {
|
|
3
|
+
try {
|
|
4
|
+
const [, payload] = token.split(".");
|
|
5
|
+
if (!payload)
|
|
6
|
+
return {};
|
|
7
|
+
const json = atob(payload.replace(/-/g, "+").replace(/_/g, "/"));
|
|
8
|
+
return JSON.parse(json);
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return {};
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
class TokenProvider {
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.cached = null;
|
|
17
|
+
this.inflight = null;
|
|
18
|
+
this.issuerUrl = options.issuerUrl.replace(/\/$/, "");
|
|
19
|
+
this.clientId = options.clientId;
|
|
20
|
+
this.clientSecret = options.clientSecret;
|
|
21
|
+
}
|
|
22
|
+
async ensureFresh() {
|
|
23
|
+
const now = Date.now();
|
|
24
|
+
if (this.cached && this.cached.expiresAt - now > 30000)
|
|
25
|
+
return this.cached;
|
|
26
|
+
if (this.inflight)
|
|
27
|
+
return this.inflight;
|
|
28
|
+
this.inflight = this.fetchToken().finally(() => { this.inflight = null; });
|
|
29
|
+
return this.inflight;
|
|
30
|
+
}
|
|
31
|
+
getToken() {
|
|
32
|
+
return this.ensureFresh().then((c) => c.value);
|
|
33
|
+
}
|
|
34
|
+
getOrganizationId() {
|
|
35
|
+
return this.ensureFresh().then((c) => c.organizationId);
|
|
36
|
+
}
|
|
37
|
+
async fetchToken() {
|
|
38
|
+
const credentials = btoa(`${this.clientId}:${this.clientSecret}`);
|
|
39
|
+
const res = await fetch(`${this.issuerUrl}/auth/token`, {
|
|
40
|
+
method: "POST",
|
|
41
|
+
headers: {
|
|
42
|
+
"Content-Type": "application/json",
|
|
43
|
+
Authorization: `Basic ${credentials}`,
|
|
44
|
+
},
|
|
45
|
+
body: JSON.stringify({ grant_type: "service_account" }),
|
|
46
|
+
cache: "no-store",
|
|
47
|
+
});
|
|
48
|
+
if (!res.ok) {
|
|
49
|
+
const err = await res.json().catch(() => ({}));
|
|
50
|
+
throw new Error(`GeekappsPost auth failed (${res.status}): ${err.error ?? "unknown error"}`);
|
|
51
|
+
}
|
|
52
|
+
const data = await res.json();
|
|
53
|
+
const claims = decodeJwtPayload(data.access_token);
|
|
54
|
+
const organizationId = typeof claims.organizationId === "string" ? claims.organizationId :
|
|
55
|
+
typeof claims.organization_id === "string" ? claims.organization_id :
|
|
56
|
+
undefined;
|
|
57
|
+
this.cached = {
|
|
58
|
+
value: data.access_token,
|
|
59
|
+
expiresAt: Date.now() + (data.expires_in ?? 420) * 1000,
|
|
60
|
+
organizationId,
|
|
61
|
+
};
|
|
62
|
+
return this.cached;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export class GeekappsPost extends PostClient {
|
|
66
|
+
constructor(options) {
|
|
67
|
+
const baseUrl = options.baseUrl ?? "https://post-api.geekapps.com.br";
|
|
68
|
+
const provider = new TokenProvider(options);
|
|
69
|
+
super({
|
|
70
|
+
baseUrl,
|
|
71
|
+
token: () => provider.getToken(),
|
|
72
|
+
organizationId: () => provider.getOrganizationId(),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=geekapps-post.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geekapps-post.js","sourceRoot":"","sources":["../src/geekapps-post.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAezC,SAAS,gBAAgB,CAAC,KAAa;IACrC,IAAI,CAAC;QACH,MAAM,CAAC,EAAE,OAAO,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACrC,IAAI,CAAC,OAAO;YAAE,OAAO,EAAE,CAAC;QACxB,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,G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E,GAAG,CAAC,IAAI,CAAC,UAAU,IAAI,GAAG,CAAC,GAAG,IAAI;YACvD,cAAc;SACf,CAAC;QACF,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF;AAED,MAAM,OAAO,YAAa,SAAQ,UAAU;IAC1C,YAAY,OAA4B;QACtC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,kCAAkC,CAAC;QACtE,MAAM,QAAQ,GAAG,IAAI,aAAa,CAAC,OAAO,CAAC,CAAC;QAE5C,KAAK,CAAC;YACJ,OAAO;YACP,KAAK,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,QAAQ,EAAE;YAChC,cAAc,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,iBAAiB,EAAqB;SACtE,CAAC,CAAC;IACL,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
+
export { GeekappsPost } from "./geekapps-post.js";
|
|
2
|
+
export type { GeekappsPostOptions } from "./geekapps-post.js";
|
|
1
3
|
export { PostClient } from "./client.js";
|
|
2
4
|
export { PostApiError } from "./errors.js";
|
|
3
|
-
export { HttpClient } from "./http.js";
|
|
4
5
|
export type { HttpClientOptions, TokenResolver } from "./http.js";
|
|
5
|
-
export { ServiceAccountTokenProvider, createServiceAccountToken } from "./service-account.js";
|
|
6
|
-
export type { ServiceAccountOptions } from "./service-account.js";
|
|
7
6
|
export { DomainsResource } from "./resources/domains.js";
|
|
8
7
|
export { HistoryResource } from "./resources/history.js";
|
|
9
8
|
export { ManagedNumbersResource } from "./resources/managed-numbers.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,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AkB,EAClB,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,UAAU,EACV,gBAAgB,EAChB,gBAAgB,EAChB,2BAA2B,EAC3B,eAAe,EACf,mBAAmB,EACnB,kBAAkB,EAClB,0BAA0B,EAC1B,2BAA2B,EAC3B,qBAAqB,EACrB,UAAU,EACV,eAAe,EACf,YAAY,EACZ,cAAc,EACd,qBAAqB,EACrB,oBAAoB,EACpB,gBAAgB,EAChB,wBAAwB,EACxB,sBAAsB,GACvB,MAAM,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
+
export { GeekappsPost } from "./geekapps-post.js";
|
|
1
2
|
export { PostClient } from "./client.js";
|
|
2
3
|
export { PostApiError } from "./errors.js";
|
|
3
|
-
export { HttpClient } from "./http.js";
|
|
4
|
-
export { ServiceAccountTokenProvider, createServiceAccountToken } from "./service-account.js";
|
|
5
4
|
export { DomainsResource } from "./resources/domains.js";
|
|
6
5
|
export { HistoryResource } from "./resources/history.js";
|
|
7
6
|
export { ManagedNumbersResource } from "./resources/managed-numbers.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AACxE,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,yBAAyB,EAAE,MAAM,mCAAmC,CAAC"}
|
package/dist/server-exports.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export { GeekappsPost } from "./geekapps-post.js";
|
|
2
|
+
export type { GeekappsPostOptions } from "./geekapps-post.js";
|
|
1
3
|
export { createPostClient } from "./server.js";
|
|
2
4
|
export type { PostNextjsOptions, PostNextjsServiceAccountOptions, CreatePostClientOptions } from "./server.js";
|
|
3
5
|
export { PostClient } from "./client.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server-exports.d.ts","sourceRoot":"","sources":["../src/server-exports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,YAAY,EAAE,iBAAiB,EAAE,+BAA+B,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAE/G,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,YAAY,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAClE,OAAO,EAAE,2BAA2B,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AAC9F,YAAY,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"server-exports.d.ts","sourceRoot":"","sources":["../src/server-exports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,YAAY,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,YAAY,EAAE,iBAAiB,EAAE,+BAA+B,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAE/G,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,YAAY,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAClE,OAAO,EAAE,2BAA2B,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AAC9F,YAAY,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC"}
|
package/dist/server-exports.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server-exports.js","sourceRoot":"","sources":["../src/server-exports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAG/C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAE,2BAA2B,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"server-exports.js","sourceRoot":"","sources":["../src/server-exports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAG/C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAE,2BAA2B,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekapps/post-nextjs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Geekapps Post SDK for Next.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -10,14 +10,6 @@
|
|
|
10
10
|
".": {
|
|
11
11
|
"import": "./dist/index.js",
|
|
12
12
|
"types": "./dist/index.d.ts"
|
|
13
|
-
},
|
|
14
|
-
"./server": {
|
|
15
|
-
"import": "./dist/server-exports.js",
|
|
16
|
-
"types": "./dist/server-exports.d.ts"
|
|
17
|
-
},
|
|
18
|
-
"./client": {
|
|
19
|
-
"import": "./dist/react-exports.js",
|
|
20
|
-
"types": "./dist/react-exports.d.ts"
|
|
21
13
|
}
|
|
22
14
|
},
|
|
23
15
|
"scripts": {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { PostClient } from "./client.js";
|
|
2
2
|
|
|
3
|
-
export interface
|
|
3
|
+
export interface GeekappsPostOptions {
|
|
4
4
|
issuerUrl: string;
|
|
5
5
|
clientId: string;
|
|
6
6
|
clientSecret: string;
|
|
7
|
-
|
|
7
|
+
baseUrl?: string;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
interface CachedToken {
|
|
@@ -24,26 +24,22 @@ function decodeJwtPayload(token: string): Record<string, unknown> {
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
class TokenProvider {
|
|
28
28
|
private readonly issuerUrl: string;
|
|
29
29
|
private readonly clientId: string;
|
|
30
30
|
private readonly clientSecret: string;
|
|
31
|
-
private readonly refreshBeforeMs: number;
|
|
32
31
|
private cached: CachedToken | null = null;
|
|
33
32
|
private inflight: Promise<CachedToken> | null = null;
|
|
34
33
|
|
|
35
|
-
constructor(options:
|
|
34
|
+
constructor(options: GeekappsPostOptions) {
|
|
36
35
|
this.issuerUrl = options.issuerUrl.replace(/\/$/, "");
|
|
37
36
|
this.clientId = options.clientId;
|
|
38
37
|
this.clientSecret = options.clientSecret;
|
|
39
|
-
this.refreshBeforeMs = (options.refreshBeforeExpirySeconds ?? 30) * 1000;
|
|
40
38
|
}
|
|
41
39
|
|
|
42
40
|
private async ensureFresh(): Promise<CachedToken> {
|
|
43
41
|
const now = Date.now();
|
|
44
|
-
if (this.cached && this.cached.expiresAt - now > this.
|
|
45
|
-
return this.cached;
|
|
46
|
-
}
|
|
42
|
+
if (this.cached && this.cached.expiresAt - now > 30_000) return this.cached;
|
|
47
43
|
if (this.inflight) return this.inflight;
|
|
48
44
|
this.inflight = this.fetchToken().finally(() => { this.inflight = null; });
|
|
49
45
|
return this.inflight;
|
|
@@ -59,7 +55,6 @@ export class ServiceAccountTokenProvider {
|
|
|
59
55
|
|
|
60
56
|
private async fetchToken(): Promise<CachedToken> {
|
|
61
57
|
const credentials = btoa(`${this.clientId}:${this.clientSecret}`);
|
|
62
|
-
|
|
63
58
|
const res = await fetch(`${this.issuerUrl}/auth/token`, {
|
|
64
59
|
method: "POST",
|
|
65
60
|
headers: {
|
|
@@ -72,7 +67,7 @@ export class ServiceAccountTokenProvider {
|
|
|
72
67
|
|
|
73
68
|
if (!res.ok) {
|
|
74
69
|
const err = await res.json().catch(() => ({})) as { error?: string };
|
|
75
|
-
throw new Error(`
|
|
70
|
+
throw new Error(`GeekappsPost auth failed (${res.status}): ${err.error ?? "unknown error"}`);
|
|
76
71
|
}
|
|
77
72
|
|
|
78
73
|
const data = await res.json() as { access_token: string; expires_in?: number };
|
|
@@ -82,16 +77,24 @@ export class ServiceAccountTokenProvider {
|
|
|
82
77
|
typeof claims.organization_id === "string" ? claims.organization_id :
|
|
83
78
|
undefined;
|
|
84
79
|
|
|
85
|
-
|
|
86
|
-
|
|
80
|
+
this.cached = {
|
|
81
|
+
value: data.access_token,
|
|
82
|
+
expiresAt: Date.now() + (data.expires_in ?? 420) * 1000,
|
|
83
|
+
organizationId,
|
|
84
|
+
};
|
|
87
85
|
return this.cached;
|
|
88
86
|
}
|
|
89
|
-
|
|
90
|
-
asTokenResolver(): TokenResolver {
|
|
91
|
-
return () => this.getToken();
|
|
92
|
-
}
|
|
93
87
|
}
|
|
94
88
|
|
|
95
|
-
export
|
|
96
|
-
|
|
89
|
+
export class GeekappsPost extends PostClient {
|
|
90
|
+
constructor(options: GeekappsPostOptions) {
|
|
91
|
+
const baseUrl = options.baseUrl ?? "https://post-api.geekapps.com.br";
|
|
92
|
+
const provider = new TokenProvider(options);
|
|
93
|
+
|
|
94
|
+
super({
|
|
95
|
+
baseUrl,
|
|
96
|
+
token: () => provider.getToken(),
|
|
97
|
+
organizationId: () => provider.getOrganizationId() as Promise<string>,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
97
100
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
+
export { GeekappsPost } from "./geekapps-post.js";
|
|
2
|
+
export type { GeekappsPostOptions } from "./geekapps-post.js";
|
|
1
3
|
export { PostClient } from "./client.js";
|
|
2
4
|
export { PostApiError } from "./errors.js";
|
|
3
|
-
export { HttpClient } from "./http.js";
|
|
4
5
|
export type { HttpClientOptions, TokenResolver } from "./http.js";
|
|
5
6
|
|
|
6
|
-
export { ServiceAccountTokenProvider, createServiceAccountToken } from "./service-account.js";
|
|
7
|
-
export type { ServiceAccountOptions } from "./service-account.js";
|
|
8
|
-
|
|
9
7
|
export { DomainsResource } from "./resources/domains.js";
|
|
10
8
|
export { HistoryResource } from "./resources/history.js";
|
|
11
9
|
export { ManagedNumbersResource } from "./resources/managed-numbers.js";
|
package/src/react-exports.ts
DELETED
package/src/react.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
3
|
-
import { useState, useCallback } from "react";
|
|
4
|
-
import { PostClient } from "./client.js";
|
|
5
|
-
import type { HttpClientOptions } from "./http.js";
|
|
6
|
-
|
|
7
|
-
export interface UsePostOptions extends HttpClientOptions {}
|
|
8
|
-
|
|
9
|
-
export function usePost(options: UsePostOptions): PostClient {
|
|
10
|
-
const [client] = useState(() => new PostClient(options));
|
|
11
|
-
return client;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface UsePostActionState<T> {
|
|
15
|
-
data: T | null;
|
|
16
|
-
error: Error | null;
|
|
17
|
-
loading: boolean;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function usePostAction<T>(
|
|
21
|
-
action: (client: PostClient) => Promise<T>,
|
|
22
|
-
client: PostClient,
|
|
23
|
-
): [UsePostActionState<T>, () => void] {
|
|
24
|
-
const [state, setState] = useState<UsePostActionState<T>>({
|
|
25
|
-
data: null,
|
|
26
|
-
error: null,
|
|
27
|
-
loading: false,
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
const run = useCallback(() => {
|
|
31
|
-
setState({ data: null, error: null, loading: true });
|
|
32
|
-
action(client)
|
|
33
|
-
.then((data) => setState({ data, error: null, loading: false }))
|
|
34
|
-
.catch((error: Error) => setState({ data: null, error, loading: false }));
|
|
35
|
-
}, [action, client]);
|
|
36
|
-
|
|
37
|
-
return [state, run];
|
|
38
|
-
}
|
package/src/server-exports.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export { createPostClient } from "./server.js";
|
|
2
|
-
export type { PostNextjsOptions, PostNextjsServiceAccountOptions, CreatePostClientOptions } from "./server.js";
|
|
3
|
-
|
|
4
|
-
export { PostClient } from "./client.js";
|
|
5
|
-
export { PostApiError } from "./errors.js";
|
|
6
|
-
export type { HttpClientOptions, TokenResolver } from "./http.js";
|
|
7
|
-
export { ServiceAccountTokenProvider, createServiceAccountToken } from "./service-account.js";
|
|
8
|
-
export type { ServiceAccountOptions } from "./service-account.js";
|
package/src/server.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { PostClient } from "./client.js";
|
|
2
|
-
import { ServiceAccountTokenProvider, type ServiceAccountOptions } from "./service-account.js";
|
|
3
|
-
import type { TokenResolver } from "./http.js";
|
|
4
|
-
|
|
5
|
-
export interface PostNextjsServiceAccountOptions {
|
|
6
|
-
baseUrl?: string;
|
|
7
|
-
serviceAccount: ServiceAccountOptions;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface PostNextjsOptions {
|
|
11
|
-
baseUrl?: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export type CreatePostClientOptions = PostNextjsServiceAccountOptions | PostNextjsOptions;
|
|
15
|
-
|
|
16
|
-
function isServiceAccountOptions(opts: CreatePostClientOptions): opts is PostNextjsServiceAccountOptions {
|
|
17
|
-
return "serviceAccount" in opts;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Creates a PostClient for use in Server Components, Route Handlers, and Server Actions.
|
|
22
|
-
*
|
|
23
|
-
* Two modes:
|
|
24
|
-
* - Service account: pass `serviceAccount` credentials, the SDK handles token acquisition internally
|
|
25
|
-
* - Token per request: call with no token config, pass `{ token }` in each method call
|
|
26
|
-
*/
|
|
27
|
-
export function createPostClient(options?: CreatePostClientOptions): PostClient {
|
|
28
|
-
const baseUrl = options?.baseUrl ?? process.env.POST_API_URL ?? "https://api.usepost.com.br";
|
|
29
|
-
|
|
30
|
-
if (options && isServiceAccountOptions(options)) {
|
|
31
|
-
const provider = new ServiceAccountTokenProvider(options.serviceAccount);
|
|
32
|
-
return new PostClient({
|
|
33
|
-
baseUrl,
|
|
34
|
-
token: () => provider.getToken(),
|
|
35
|
-
organizationId: () => provider.getOrganizationId() as Promise<string>,
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return new PostClient({ baseUrl });
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export type { TokenResolver, ServiceAccountOptions };
|