@pikku/core 0.6.23 → 0.6.25
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/CHANGELOG.md +12 -0
- package/dist/channel/channel-runner.d.ts +2 -2
- package/dist/channel/channel-runner.js +5 -5
- package/dist/channel/channel.types.d.ts +1 -1
- package/dist/channel/local/local-channel-runner.d.ts +1 -1
- package/dist/channel/local/local-channel-runner.js +7 -7
- package/dist/channel/serverless/serverless-channel-runner.d.ts +1 -1
- package/dist/channel/serverless/serverless-channel-runner.js +6 -6
- package/dist/http/http-route-runner.d.ts +1 -1
- package/dist/http/http-route-runner.js +15 -14
- package/dist/http/http-routes.types.d.ts +1 -1
- package/dist/http/pikku-fetch-http-request.d.ts +1 -0
- package/dist/http/pikku-fetch-http-request.js +64 -8
- package/dist/http/pikku-fetch-http-response.d.ts +2 -1
- package/dist/http/pikku-fetch-http-response.js +5 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/middleware/auth-apikey.js +2 -2
- package/dist/middleware/auth-bearer.js +2 -2
- package/dist/middleware/auth-cookie.d.ts +7 -3
- package/dist/middleware/auth-cookie.js +34 -17
- package/dist/middleware-runner.d.ts +3 -3
- package/dist/middleware-runner.js +2 -2
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +4 -1
- package/dist/services/jwt-service.d.ts +2 -1
- package/dist/services/local-secrets.d.ts +2 -2
- package/dist/services/local-secrets.js +5 -5
- package/dist/services/user-session-service.d.ts +2 -0
- package/dist/services/user-session-service.js +3 -0
- package/dist/time-utils.d.ts +12 -0
- package/dist/time-utils.js +18 -0
- package/dist/types/core.types.d.ts +5 -5
- package/dist/types/functions.types.d.ts +1 -1
- package/lcov.info +1767 -1031
- package/package.json +1 -1
- package/src/channel/channel-runner.ts +6 -6
- package/src/channel/channel.types.ts +1 -1
- package/src/channel/local/local-channel-runner.test.ts +0 -2
- package/src/channel/local/local-channel-runner.ts +7 -7
- package/src/channel/serverless/serverless-channel-runner.ts +6 -9
- package/src/http/http-route-runner.test.ts +1 -1
- package/src/http/http-route-runner.ts +25 -17
- package/src/http/http-routes.types.ts +1 -1
- package/src/http/pikku-fetch-http-request.test.ts +237 -0
- package/src/http/pikku-fetch-http-request.ts +64 -8
- package/src/http/pikku-fetch-http-response.test.ts +82 -0
- package/src/http/pikku-fetch-http-response.ts +16 -2
- package/src/index.ts +1 -1
- package/src/middleware/auth-apikey.ts +2 -2
- package/src/middleware/auth-bearer.ts +2 -2
- package/src/middleware/auth-cookie.ts +51 -21
- package/src/middleware-runner.ts +3 -3
- package/src/schema.test.ts +6 -6
- package/src/schema.ts +3 -1
- package/src/services/jwt-service.ts +6 -1
- package/src/services/local-secrets.ts +3 -3
- package/src/services/user-session-service.ts +4 -0
- package/src/time-utils.test.ts +56 -0
- package/src/time-utils.ts +32 -0
- package/src/types/core.types.ts +5 -5
- package/src/types/functions.types.ts +1 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RelativeTimeInput } from '../time-utils.js';
|
|
1
2
|
/**
|
|
2
3
|
* Interface for handling JSON Web Tokens (JWT).
|
|
3
4
|
*/
|
|
@@ -8,7 +9,7 @@ export interface JWTService {
|
|
|
8
9
|
* @param payload - The payload to encode.
|
|
9
10
|
* @returns A promise that resolves to the encoded JWT.
|
|
10
11
|
*/
|
|
11
|
-
encode: <T extends any>(expiresIn:
|
|
12
|
+
encode: <T extends any>(expiresIn: RelativeTimeInput, payload: T) => Promise<string>;
|
|
12
13
|
/**
|
|
13
14
|
* Decodes a JWT into its payload.
|
|
14
15
|
* @param hash - The JWT to decode.
|
|
@@ -4,11 +4,11 @@ import { VariablesService } from './variables-service.js';
|
|
|
4
4
|
* Service for retrieving secrets from environment variables.
|
|
5
5
|
*/
|
|
6
6
|
export declare class LocalSecretService implements SecretService {
|
|
7
|
-
private
|
|
7
|
+
private variables;
|
|
8
8
|
/**
|
|
9
9
|
* Creates an instance of LocalSecretService.
|
|
10
10
|
*/
|
|
11
|
-
constructor(
|
|
11
|
+
constructor(variables?: VariablesService);
|
|
12
12
|
/**
|
|
13
13
|
* Retrieves a secret by key.
|
|
14
14
|
* @param key - The key of the secret to retrieve.
|
|
@@ -3,12 +3,12 @@ import { LocalVariablesService } from './local-variables.js';
|
|
|
3
3
|
* Service for retrieving secrets from environment variables.
|
|
4
4
|
*/
|
|
5
5
|
export class LocalSecretService {
|
|
6
|
-
|
|
6
|
+
variables;
|
|
7
7
|
/**
|
|
8
8
|
* Creates an instance of LocalSecretService.
|
|
9
9
|
*/
|
|
10
|
-
constructor(
|
|
11
|
-
this.
|
|
10
|
+
constructor(variables = new LocalVariablesService()) {
|
|
11
|
+
this.variables = variables;
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* Retrieves a secret by key.
|
|
@@ -17,7 +17,7 @@ export class LocalSecretService {
|
|
|
17
17
|
* @throws {Error} If the secret is not found.
|
|
18
18
|
*/
|
|
19
19
|
async getSecretJSON(key) {
|
|
20
|
-
const value = await this.
|
|
20
|
+
const value = await this.variables.get(key);
|
|
21
21
|
if (value) {
|
|
22
22
|
return JSON.parse(value);
|
|
23
23
|
}
|
|
@@ -30,7 +30,7 @@ export class LocalSecretService {
|
|
|
30
30
|
* @throws {Error} If the secret is not found.
|
|
31
31
|
*/
|
|
32
32
|
async getSecret(key) {
|
|
33
|
-
const value = await this.
|
|
33
|
+
const value = await this.variables.get(key);
|
|
34
34
|
if (value) {
|
|
35
35
|
return value;
|
|
36
36
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ChannelStore } from '../channel/channel-store.js';
|
|
2
2
|
import { CoreUserSession } from '../types/core.types.js';
|
|
3
3
|
export interface UserSessionService<UserSession extends CoreUserSession> {
|
|
4
|
+
sessionChanged: boolean;
|
|
4
5
|
setInitial(session: UserSession): void;
|
|
5
6
|
set(session: UserSession): Promise<void> | void;
|
|
6
7
|
clear(): Promise<void> | void;
|
|
@@ -9,6 +10,7 @@ export interface UserSessionService<UserSession extends CoreUserSession> {
|
|
|
9
10
|
export declare class PikkuUserSessionService<UserSession extends CoreUserSession> implements UserSessionService<UserSession> {
|
|
10
11
|
private channelStore?;
|
|
11
12
|
private channelId?;
|
|
13
|
+
sessionChanged: boolean;
|
|
12
14
|
private session;
|
|
13
15
|
constructor(channelStore?: ChannelStore<unknown, unknown, UserSession> | undefined, channelId?: string | undefined);
|
|
14
16
|
setInitial(session: UserSession): void;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export class PikkuUserSessionService {
|
|
2
2
|
channelStore;
|
|
3
3
|
channelId;
|
|
4
|
+
sessionChanged = false;
|
|
4
5
|
session;
|
|
5
6
|
constructor(channelStore, channelId) {
|
|
6
7
|
this.channelStore = channelStore;
|
|
@@ -13,10 +14,12 @@ export class PikkuUserSessionService {
|
|
|
13
14
|
this.session = session;
|
|
14
15
|
}
|
|
15
16
|
set(session) {
|
|
17
|
+
this.sessionChanged = true;
|
|
16
18
|
this.session = session;
|
|
17
19
|
return this.channelStore?.setUserSession(this.channelId, session);
|
|
18
20
|
}
|
|
19
21
|
clear() {
|
|
22
|
+
this.sessionChanged = true;
|
|
20
23
|
this.session = undefined;
|
|
21
24
|
return this.channelStore?.setUserSession(this.channelId, null);
|
|
22
25
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type TimeUnit = 'second' | 'minute' | 'hour' | 'day' | 'week' | 'year';
|
|
2
|
+
export interface RelativeTimeInput {
|
|
3
|
+
value: number;
|
|
4
|
+
unit: TimeUnit;
|
|
5
|
+
}
|
|
6
|
+
export declare const getRelativeTimeOffset: ({ value, unit, }: RelativeTimeInput) => number;
|
|
7
|
+
/**
|
|
8
|
+
* Returns a Unix timestamp (in seconds) offset from now by the given duration.
|
|
9
|
+
* e.g. value = -1 and unit = 'day' => 1 day ago => now - 86400 seconds
|
|
10
|
+
*/
|
|
11
|
+
export declare const getRelativeTimeOffsetFromNow: (relativeTime: RelativeTimeInput) => Date;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const multipliers = {
|
|
2
|
+
second: 1,
|
|
3
|
+
minute: 60,
|
|
4
|
+
hour: 60 * 60,
|
|
5
|
+
day: 60 * 60 * 24,
|
|
6
|
+
week: 60 * 60 * 24 * 7,
|
|
7
|
+
year: Math.round(365.25 * 60 * 60 * 24),
|
|
8
|
+
};
|
|
9
|
+
export const getRelativeTimeOffset = ({ value, unit, }) => {
|
|
10
|
+
return Math.round(value * multipliers[unit]) * 1000; // convert to milliseconds
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Returns a Unix timestamp (in seconds) offset from now by the given duration.
|
|
14
|
+
* e.g. value = -1 and unit = 'day' => 1 day ago => now - 86400 seconds
|
|
15
|
+
*/
|
|
16
|
+
export const getRelativeTimeOffsetFromNow = (relativeTime) => {
|
|
17
|
+
return new Date(Date.now() + getRelativeTimeOffset(relativeTime));
|
|
18
|
+
};
|
|
@@ -52,17 +52,17 @@ export interface CoreSingletonServices<Config extends CoreConfig = CoreConfig> {
|
|
|
52
52
|
/** JWT Service */
|
|
53
53
|
jwt?: JWTService;
|
|
54
54
|
/** The schema library used to validate data */
|
|
55
|
-
|
|
55
|
+
schema?: SchemaService;
|
|
56
56
|
/** The core configuration for the application. */
|
|
57
57
|
config: Config;
|
|
58
58
|
/** The logger used by the application. */
|
|
59
59
|
logger: Logger;
|
|
60
60
|
/** The variable service to be used */
|
|
61
|
-
|
|
61
|
+
variables: VariablesService;
|
|
62
62
|
/** The subscription service that is passed to streams */
|
|
63
63
|
eventHub?: EventHubService<unknown>;
|
|
64
64
|
/** SecretServce */
|
|
65
|
-
|
|
65
|
+
secrets?: SecretService;
|
|
66
66
|
}
|
|
67
67
|
/**
|
|
68
68
|
* Represents different forms of interaction within Pikku and the outside world.
|
|
@@ -74,7 +74,7 @@ export interface PikkuInteraction {
|
|
|
74
74
|
* A function that can wrap an interaction and be called before or after
|
|
75
75
|
*/
|
|
76
76
|
export type PikkuMiddleware<SingletonServices extends CoreSingletonServices = CoreSingletonServices, UserSession extends CoreUserSession = CoreUserSession> = (services: SingletonServices & {
|
|
77
|
-
|
|
77
|
+
userSession: UserSessionService<UserSession>;
|
|
78
78
|
}, interactions: PikkuInteraction, next: () => Promise<void>) => Promise<void>;
|
|
79
79
|
/**
|
|
80
80
|
* Represents the core services used by Pikku, including singleton services and the request/response interaction.
|
|
@@ -94,7 +94,7 @@ export type CreateSessionServices<SingletonServices extends CoreSingletonService
|
|
|
94
94
|
/**
|
|
95
95
|
* Defines a function type for creating config.
|
|
96
96
|
*/
|
|
97
|
-
export type CreateConfig<Config extends CoreConfig> = (
|
|
97
|
+
export type CreateConfig<Config extends CoreConfig> = (variables?: VariablesService) => Promise<Config>;
|
|
98
98
|
/**
|
|
99
99
|
* Represents the documentation for a route, including summary, description, tags, and errors.
|
|
100
100
|
*/
|
|
@@ -21,7 +21,7 @@ export type CoreAPIFunction<In, Out, Services extends CoreSingletonServices = Co
|
|
|
21
21
|
*/
|
|
22
22
|
export type CoreAPIFunctionSessionless<In, Out, Services extends CoreSingletonServices = CoreServices & {
|
|
23
23
|
channel?: PikkuChannel<unknown, Out>;
|
|
24
|
-
}, Session extends CoreUserSession = CoreUserSession, Channel extends boolean = false> = (services: Services, data: In, session
|
|
24
|
+
}, Session extends CoreUserSession = CoreUserSession, Channel extends boolean = false> = (services: Services, data: In, session?: Session) => Channel extends true ? Promise<Out> | Promise<void> : Promise<Out>;
|
|
25
25
|
/**
|
|
26
26
|
* Represents a function that checks permissions for a given API operation.
|
|
27
27
|
*
|