celestya 0.0.5 → 0.2.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/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { IronSessionData, IronSession } from 'iron-session';
1
+ import { SessionOptions, IronSessionData, IronSession } from 'iron-session';
2
2
  import { NextRequest } from 'next/server';
3
3
 
4
4
  declare module "iron-session" {
@@ -12,19 +12,86 @@ declare module "iron-session" {
12
12
  }
13
13
  }
14
14
  interface DefaultUser {
15
- [key: string]: any;
15
+ [key: string]: unknown;
16
16
  }
17
17
  interface Token {
18
18
  token: string;
19
19
  }
20
+ declare const getSession: <U = DefaultUser>(sessionOpts?: SessionOptions) => Promise<Session<U>>;
21
+
22
+ type BaseError = {
23
+ error: string;
24
+ message: string;
25
+ };
26
+ type Success<S> = {
27
+ data: S;
28
+ };
29
+ /**
30
+ * A Result type that represents either a successful value (Ok) or an error (Err).
31
+ * This is a discriminated union type that helps handle errors in a type-safe way.
32
+ *
33
+ * @template T - The type of the successful value
34
+ * @template E - The type of the error, must extend BaseError
35
+ */
36
+ type Result<T, E extends BaseError> = Ok<T, E> | Err<T, E>;
37
+ type IResult<T, E extends BaseError> = {
38
+ /**
39
+ * Checks if the `Result` is an `Ok` instance
40
+ */
41
+ isOk: () => this is Ok<T, E>;
42
+ /**
43
+ * Checks if the `Result` is an `Err` instance.
44
+ */
45
+ isErr: () => this is Err<T, E>;
46
+ };
47
+ /**
48
+ * Represents a successful `Result` value.
49
+ *
50
+ * @template T - The type of the successful value.
51
+ * @template E - The type of the error, must extend `BaseError`.
52
+ */
53
+ declare class Ok<T, E extends BaseError> implements IResult<T, E> {
54
+ value: Success<T>;
55
+ constructor(value: Success<T>);
56
+ isOk(): this is Ok<T, E>;
57
+ isErr(): this is Err<T, E>;
58
+ }
59
+ /**
60
+ * Represents an error `Result` value.
61
+ *
62
+ * @template T - The type of the successful value.
63
+ * @template E - The type of the error, must extend `BaseError`.
64
+ */
65
+ declare class Err<T, E extends BaseError> implements IResult<T, E> {
66
+ error: E;
67
+ constructor(error: E);
68
+ isOk(): this is Ok<T, E>;
69
+ isErr(): this is Err<T, E>;
70
+ }
71
+ /**
72
+ * Creates a new successful `Result` (i.e., an instance of `Ok`).
73
+ *
74
+ * @template T - The type of the successful value
75
+ * @param value - The successful value
76
+ * @returns A new `Ok` instance.
77
+ */
78
+ declare function ok<const T>(value: Success<T>): Result<T, never>;
79
+ /**
80
+ * Creates a new error `Result` (i.e., an instance of `Err`).
81
+ *
82
+ * @template E - The type of the error, must extend `BaseError`.
83
+ * @param error - The error value.
84
+ * @returns A new `Err` instance.
85
+ */
86
+ declare function err<const E extends BaseError>(error: E): Result<never, E>;
20
87
 
21
88
  type ServerSideSession<U> = IronSessionData<U, Token>;
22
89
 
23
90
  type Params = string[];
24
91
  interface IRequestOptions {
25
- params: {
92
+ params: Promise<{
26
93
  endpoint: Params;
27
- };
94
+ }>;
28
95
  }
29
96
  interface IConfig {
30
97
  host: string;
@@ -34,16 +101,48 @@ interface IConfig {
34
101
  debug?: boolean;
35
102
  }
36
103
  type Session<U = DefaultUser> = IronSession<ServerSideSession<U>>;
37
- interface IServerSideRequestOptions {
38
- method?: string;
39
- body?: JSON;
40
- }
104
+ type CallbackOptions = {
105
+ method: "GET" | "POST" | "DELETE";
106
+ url: string;
107
+ body?: object;
108
+ };
109
+ type WrapperFunction = <T>(data: CallbackOptions) => Promise<Result<T, BaseError>>;
110
+
111
+ declare const CelestyaProxy: (config: IConfig) => {
112
+ POST: (req: NextRequest, opt: IRequestOptions) => Promise<any>;
113
+ GET: (req: NextRequest, opt: IRequestOptions) => Promise<any>;
114
+ DELETE: (req: NextRequest, opt: IRequestOptions) => Promise<any>;
115
+ };
41
116
 
42
- declare function proxy(method: string, request: NextRequest, options: IRequestOptions, config: IConfig): Promise<any>;
117
+ declare const serverSideFetch: <T>({ url, method, config, body, sessionIsOptional, skipRefresh, ...options }: {
118
+ url: string;
119
+ method?: "GET" | "POST" | "DELETE";
120
+ body?: object;
121
+ config: IConfig;
122
+ sessionIsOptional?: boolean;
123
+ skipRefresh?: boolean;
124
+ } & Omit<RequestInit, "body" | "method">) => Promise<Result<T, BaseError>>;
125
+ declare function attemptTokenRefresh(config: IConfig): Promise<Result<string, BaseError>>;
43
126
 
44
- declare const Proxy: typeof proxy;
45
- declare const getSession: <U = DefaultUser>() => Promise<Session<U>>;
46
- declare const debug: () => void;
47
- declare const apiFetch: (url: string, options: IServerSideRequestOptions, config: IConfig) => Promise<any>;
127
+ /**
128
+ * Register new Wrapper for the API.
129
+ *
130
+ * Example Wrapper:
131
+ * ```ts
132
+ * const apiWrapper = (cb: WrapperFunction) => {
133
+ * return {
134
+ * commmands: {
135
+ * get: () => cb<string>({ method: "GET", url: "/command" }),
136
+ * update: (id: string, body: JSON) =>
137
+ * cb({ method: "POST", url: `/command/${id}`, body }),
138
+ * },
139
+ * };
140
+ * };
141
+ * ````
142
+ * @param apiWrapper
143
+ * @param config
144
+ * @returns
145
+ */
146
+ declare const serverAPIWrapper: <T>(apiWrapper: (cb: WrapperFunction) => T, config: IConfig) => T;
48
147
 
49
- export { type IConfig, type IRequestOptions, Proxy, type Session, apiFetch, debug, getSession };
148
+ export { type BaseError, CelestyaProxy, Err, type IConfig, type IRequestOptions, Ok, type Result, type Session, type WrapperFunction, attemptTokenRefresh, err, getSession, ok, serverAPIWrapper, serverSideFetch };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IronSessionData, IronSession } from 'iron-session';
1
+ import { SessionOptions, IronSessionData, IronSession } from 'iron-session';
2
2
  import { NextRequest } from 'next/server';
3
3
 
4
4
  declare module "iron-session" {
@@ -12,19 +12,86 @@ declare module "iron-session" {
12
12
  }
13
13
  }
14
14
  interface DefaultUser {
15
- [key: string]: any;
15
+ [key: string]: unknown;
16
16
  }
17
17
  interface Token {
18
18
  token: string;
19
19
  }
20
+ declare const getSession: <U = DefaultUser>(sessionOpts?: SessionOptions) => Promise<Session<U>>;
21
+
22
+ type BaseError = {
23
+ error: string;
24
+ message: string;
25
+ };
26
+ type Success<S> = {
27
+ data: S;
28
+ };
29
+ /**
30
+ * A Result type that represents either a successful value (Ok) or an error (Err).
31
+ * This is a discriminated union type that helps handle errors in a type-safe way.
32
+ *
33
+ * @template T - The type of the successful value
34
+ * @template E - The type of the error, must extend BaseError
35
+ */
36
+ type Result<T, E extends BaseError> = Ok<T, E> | Err<T, E>;
37
+ type IResult<T, E extends BaseError> = {
38
+ /**
39
+ * Checks if the `Result` is an `Ok` instance
40
+ */
41
+ isOk: () => this is Ok<T, E>;
42
+ /**
43
+ * Checks if the `Result` is an `Err` instance.
44
+ */
45
+ isErr: () => this is Err<T, E>;
46
+ };
47
+ /**
48
+ * Represents a successful `Result` value.
49
+ *
50
+ * @template T - The type of the successful value.
51
+ * @template E - The type of the error, must extend `BaseError`.
52
+ */
53
+ declare class Ok<T, E extends BaseError> implements IResult<T, E> {
54
+ value: Success<T>;
55
+ constructor(value: Success<T>);
56
+ isOk(): this is Ok<T, E>;
57
+ isErr(): this is Err<T, E>;
58
+ }
59
+ /**
60
+ * Represents an error `Result` value.
61
+ *
62
+ * @template T - The type of the successful value.
63
+ * @template E - The type of the error, must extend `BaseError`.
64
+ */
65
+ declare class Err<T, E extends BaseError> implements IResult<T, E> {
66
+ error: E;
67
+ constructor(error: E);
68
+ isOk(): this is Ok<T, E>;
69
+ isErr(): this is Err<T, E>;
70
+ }
71
+ /**
72
+ * Creates a new successful `Result` (i.e., an instance of `Ok`).
73
+ *
74
+ * @template T - The type of the successful value
75
+ * @param value - The successful value
76
+ * @returns A new `Ok` instance.
77
+ */
78
+ declare function ok<const T>(value: Success<T>): Result<T, never>;
79
+ /**
80
+ * Creates a new error `Result` (i.e., an instance of `Err`).
81
+ *
82
+ * @template E - The type of the error, must extend `BaseError`.
83
+ * @param error - The error value.
84
+ * @returns A new `Err` instance.
85
+ */
86
+ declare function err<const E extends BaseError>(error: E): Result<never, E>;
20
87
 
21
88
  type ServerSideSession<U> = IronSessionData<U, Token>;
22
89
 
23
90
  type Params = string[];
24
91
  interface IRequestOptions {
25
- params: {
92
+ params: Promise<{
26
93
  endpoint: Params;
27
- };
94
+ }>;
28
95
  }
29
96
  interface IConfig {
30
97
  host: string;
@@ -34,16 +101,48 @@ interface IConfig {
34
101
  debug?: boolean;
35
102
  }
36
103
  type Session<U = DefaultUser> = IronSession<ServerSideSession<U>>;
37
- interface IServerSideRequestOptions {
38
- method?: string;
39
- body?: JSON;
40
- }
104
+ type CallbackOptions = {
105
+ method: "GET" | "POST" | "DELETE";
106
+ url: string;
107
+ body?: object;
108
+ };
109
+ type WrapperFunction = <T>(data: CallbackOptions) => Promise<Result<T, BaseError>>;
110
+
111
+ declare const CelestyaProxy: (config: IConfig) => {
112
+ POST: (req: NextRequest, opt: IRequestOptions) => Promise<any>;
113
+ GET: (req: NextRequest, opt: IRequestOptions) => Promise<any>;
114
+ DELETE: (req: NextRequest, opt: IRequestOptions) => Promise<any>;
115
+ };
41
116
 
42
- declare function proxy(method: string, request: NextRequest, options: IRequestOptions, config: IConfig): Promise<any>;
117
+ declare const serverSideFetch: <T>({ url, method, config, body, sessionIsOptional, skipRefresh, ...options }: {
118
+ url: string;
119
+ method?: "GET" | "POST" | "DELETE";
120
+ body?: object;
121
+ config: IConfig;
122
+ sessionIsOptional?: boolean;
123
+ skipRefresh?: boolean;
124
+ } & Omit<RequestInit, "body" | "method">) => Promise<Result<T, BaseError>>;
125
+ declare function attemptTokenRefresh(config: IConfig): Promise<Result<string, BaseError>>;
43
126
 
44
- declare const Proxy: typeof proxy;
45
- declare const getSession: <U = DefaultUser>() => Promise<Session<U>>;
46
- declare const debug: () => void;
47
- declare const apiFetch: (url: string, options: IServerSideRequestOptions, config: IConfig) => Promise<any>;
127
+ /**
128
+ * Register new Wrapper for the API.
129
+ *
130
+ * Example Wrapper:
131
+ * ```ts
132
+ * const apiWrapper = (cb: WrapperFunction) => {
133
+ * return {
134
+ * commmands: {
135
+ * get: () => cb<string>({ method: "GET", url: "/command" }),
136
+ * update: (id: string, body: JSON) =>
137
+ * cb({ method: "POST", url: `/command/${id}`, body }),
138
+ * },
139
+ * };
140
+ * };
141
+ * ````
142
+ * @param apiWrapper
143
+ * @param config
144
+ * @returns
145
+ */
146
+ declare const serverAPIWrapper: <T>(apiWrapper: (cb: WrapperFunction) => T, config: IConfig) => T;
48
147
 
49
- export { type IConfig, type IRequestOptions, Proxy, type Session, apiFetch, debug, getSession };
148
+ export { type BaseError, CelestyaProxy, Err, type IConfig, type IRequestOptions, Ok, type Result, type Session, type WrapperFunction, attemptTokenRefresh, err, getSession, ok, serverAPIWrapper, serverSideFetch };