@secrecy/lib 1.56.0 → 1.57.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.
@@ -15,21 +15,24 @@ async function getPublicUser(client, id) {
15
15
  return user;
16
16
  }
17
17
  export class BaseClient {
18
- static getBaseClient = (session, onAccessDenied) => createTRPCClient(session, onAccessDenied);
18
+ static getBaseClient = (opts = {}) => createTRPCClient(opts);
19
19
  client;
20
20
  sessionId;
21
- constructor(session, onAccessDenied) {
22
- this.sessionId = session;
23
- this.client = BaseClient.getBaseClient(session, async () => {
24
- console.log('[BASE_CLIENT - BEFORE] - Access denied');
25
- await onAccessDenied?.();
26
- console.log('[BASE_CLIENT - AFTER] - Access denied');
27
- try {
28
- await this.logout();
29
- }
30
- finally {
31
- location.reload();
32
- }
21
+ constructor(opts) {
22
+ this.sessionId = opts.session;
23
+ this.client = BaseClient.getBaseClient({
24
+ ...opts,
25
+ onAccessDenied: async () => {
26
+ console.log('[BASE_CLIENT - BEFORE] - Access denied');
27
+ await opts.onAccessDenied?.();
28
+ console.log('[BASE_CLIENT - AFTER] - Access denied');
29
+ try {
30
+ await this.logout();
31
+ }
32
+ finally {
33
+ location.reload();
34
+ }
35
+ },
33
36
  });
34
37
  }
35
38
  async logout(sessionId) {
@@ -43,8 +46,8 @@ export class BaseClient {
43
46
  async me() {
44
47
  return await this.client.user.self.query({});
45
48
  }
46
- static async getUser(userId, sessionId) {
47
- const user = await getPublicUser(this.getBaseClient(sessionId), userId);
49
+ static async getUser(userId, session) {
50
+ const user = await getPublicUser(this.getBaseClient({ session }), userId);
48
51
  return user;
49
52
  }
50
53
  async getUser(userId) {
@@ -27,14 +27,17 @@ export class SecrecyClient extends BaseClient {
27
27
  user;
28
28
  pseudonym;
29
29
  constructor(uaSession, uaKeys, uaJwt) {
30
- super(uaSession, async () => {
31
- console.log('[CLIENT] - Access denied');
32
- try {
33
- await this.logout();
34
- }
35
- catch {
36
- // ignore
37
- }
30
+ super({
31
+ session: uaSession,
32
+ onAccessDenied: async () => {
33
+ console.log('[CLIENT] - Access denied');
34
+ try {
35
+ await this.logout();
36
+ }
37
+ catch {
38
+ // ignore
39
+ }
40
+ },
38
41
  });
39
42
  this.#keys = uaKeys;
40
43
  this.cloud = new SecrecyCloudClient(this, this.#keys, this.client);
@@ -9,19 +9,7 @@ superjson.registerCustom({
9
9
  serialize: (v) => [...v],
10
10
  deserialize: (v) => Buffer.from(v),
11
11
  }, 'buffer');
12
- export const getUrl = () => {
13
- if (process.env.NEXT_PUBLIC_SECRECY_URL) {
14
- return `${process.env.NEXT_PUBLIC_SECRECY_URL}/api/trpc`;
15
- }
16
- const protocol = process.env.NEXT_PUBLIC_VERCEL_ENV !== 'development' ? 'https' : 'http';
17
- const url = process.env.NEXT_PUBLIC_VERCEL_ENV &&
18
- process.env.NEXT_PUBLIC_VERCEL_ENV !== 'production' &&
19
- process.env.NEXT_PUBLIC_IS_SECRECY_INTERNAL === 'true'
20
- ? `${protocol}://${process.env.NEXT_PUBLIC_VERCEL_URL}/api/trpc`
21
- : `https://www.secrecy.tech/api/trpc`;
22
- return url;
23
- };
24
- export const createTRPCClient = (session, onAccessDenied) => createTRPCProxyClient({
12
+ export const createTRPCClient = (opts) => createTRPCProxyClient({
25
13
  transformer: superjson,
26
14
  links: [
27
15
  loggerLink({
@@ -29,14 +17,14 @@ export const createTRPCClient = (session, onAccessDenied) => createTRPCProxyClie
29
17
  if (op.direction === 'down' &&
30
18
  isTRPCClientError(op.result) &&
31
19
  op.result.data?.code === 'UNAUTHORIZED') {
32
- void onAccessDenied?.();
20
+ void opts.onAccessDenied?.();
33
21
  }
34
22
  return (process.env.NODE_ENV === 'development' ||
35
23
  (op.direction === 'down' && op.result instanceof Error));
36
24
  },
37
25
  }),
38
26
  httpBatchLink({
39
- url: getUrl(),
27
+ url: opts.apiUrl ?? 'https://secrecy.tech/api/trpc',
40
28
  maxURLLength: 2083,
41
29
  fetch: async (input, init) => {
42
30
  const controller = new AbortController();
@@ -45,8 +33,8 @@ export const createTRPCClient = (session, onAccessDenied) => createTRPCProxyClie
45
33
  }, 20000);
46
34
  const headers = new Headers(init?.headers);
47
35
  headers.set('secrecy-lib-version', SECRECY_LIB_VERSION);
48
- if (typeof session === 'string') {
49
- headers.set('secrecy-session', session);
36
+ if (typeof opts.session === 'string') {
37
+ headers.set('secrecy-session', opts.session);
50
38
  }
51
39
  const call = fetch(input, {
52
40
  ...init,
@@ -1,15 +1,20 @@
1
- import { type ApiClient, type RouterOutputs, type RouterInputs } from './client.js';
1
+ import { type ApiClient, type RouterOutputs, type RouterInputs, CreateTrpcClientOptions } from './client.js';
2
2
  import { type InfuraNetwork, type PublicUser } from './index.js';
3
3
  import { type SelfUser } from './client/types/user.js';
4
+ export type BaseClientOptions = {
5
+ session: string;
6
+ apiUrl?: string | null | undefined;
7
+ onAccessDenied?: () => void | Promise<void>;
8
+ };
4
9
  export declare class BaseClient {
5
10
  #private;
6
- static readonly getBaseClient: (session?: string | null | undefined, onAccessDenied?: () => void | Promise<void>) => ApiClient;
11
+ static readonly getBaseClient: (opts?: CreateTrpcClientOptions) => ApiClient;
7
12
  protected client: ApiClient;
8
13
  sessionId: string;
9
- constructor(session: string, onAccessDenied?: () => void | Promise<void>);
14
+ constructor(opts: BaseClientOptions);
10
15
  logout(sessionId?: string | null | undefined): Promise<void>;
11
16
  me(): Promise<SelfUser>;
12
- static getUser(userId: string, sessionId?: string | null | undefined): Promise<PublicUser>;
17
+ static getUser(userId: string, session?: string | undefined): Promise<PublicUser>;
13
18
  getUser(userId: string): Promise<PublicUser>;
14
19
  searchUsers(search: string): Promise<PublicUser[]>;
15
20
  updateProfile(data: RouterInputs['user']['updateProfile']): Promise<Omit<SelfUser, 'account'>>;
@@ -5,8 +5,12 @@ import superjson from 'superjson';
5
5
  export type RouterInputs = inferRouterInputs<AppRouter>;
6
6
  export type RouterOutputs = inferRouterOutputs<AppRouter>;
7
7
  export declare function isTRPCClientError(cause: unknown): cause is TRPCClientError<AppRouter>;
8
- export declare const getUrl: () => string;
9
- export declare const createTRPCClient: (session?: string | null | undefined, onAccessDenied?: () => void | Promise<void>) => {
8
+ export interface CreateTrpcClientOptions {
9
+ session?: string | null | undefined;
10
+ apiUrl?: string | null | undefined;
11
+ onAccessDenied?: () => void | Promise<void> | null | undefined;
12
+ }
13
+ export declare const createTRPCClient: (opts: CreateTrpcClientOptions) => {
10
14
  account: {
11
15
  createUser: {
12
16
  mutate: import("@trpc/client").Resolver<import("@trpc/server").BuildProcedure<"mutation", {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@secrecy/lib",
3
3
  "author": "Anonymize <anonymize@gmail.com>",
4
4
  "description": "Anonymize Secrecy Library",
5
- "version": "1.56.0",
5
+ "version": "1.57.0",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/anonymize-org/lib.git"