@moodle-next/next 0.0.1

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.
@@ -0,0 +1,153 @@
1
+ type MoodleSession = {
2
+ token: string;
3
+ userId: number;
4
+ username: string;
5
+ fullName: string;
6
+ email?: string;
7
+ userPictureUrl?: string;
8
+ canManagePlatform?: boolean;
9
+ canManageOwnFiles?: boolean;
10
+ expiresAt: number;
11
+ };
12
+ type MoodleSessionConfig = {
13
+ /** Cookie name. Default: `"moodle_session"` */
14
+ cookieName?: string;
15
+ /** Session lifetime in seconds. Default: `28800` (8 hours) */
16
+ durationSeconds?: number;
17
+ /** Path to redirect unauthenticated requests to. Default: `"/"` */
18
+ loginPath?: string;
19
+ /** Path to redirect when the Moodle token has expired. Default: `"/auth/session-expired"` */
20
+ expiredSessionPath?: string;
21
+ /**
22
+ * Session encryption secret. Falls back to `process.env.APP_SESSION_SECRET`.
23
+ * Must be set before calling any session function.
24
+ */
25
+ sessionSecret?: string;
26
+ };
27
+ declare function configureMoodleSession(config: MoodleSessionConfig): void;
28
+ declare function createSession(session: Omit<MoodleSession, 'expiresAt'>): Promise<void>;
29
+ declare function getSession(): Promise<MoodleSession | null>;
30
+ declare function requireSession(): Promise<MoodleSession>;
31
+ declare function clearSession(): Promise<void>;
32
+ declare function redirectToExpiredSession(): never;
33
+ declare function redirectIfSessionExpired(error: unknown): void;
34
+ declare function clearSessionIfAuthenticationError(error: unknown): Promise<boolean>;
35
+ declare function getSessionOrUnauthorizedResponse(): Promise<{
36
+ session: MoodleSession;
37
+ response: null;
38
+ } | {
39
+ session: null;
40
+ response: Response;
41
+ }>;
42
+ declare function clearSessionAndReturnUnauthorized(): Promise<Response>;
43
+
44
+ type MoodleBranding = {
45
+ siteName: string;
46
+ siteDescription?: string;
47
+ siteUrl: string;
48
+ logoUrl?: string;
49
+ compactLogoUrl?: string;
50
+ };
51
+ declare const getMoodleBranding: () => Promise<MoodleBranding>;
52
+ declare function getSiteForceLogin(): Promise<boolean>;
53
+ declare function getSiteGlobalSearchEnabled(): Promise<boolean>;
54
+ declare function getSiteMessagingEnabled(): Promise<boolean>;
55
+
56
+ type GetSession$1 = () => Promise<{
57
+ token: string;
58
+ } | null>;
59
+ type OnAuthFailure$1 = () => Promise<Response> | Response;
60
+ /**
61
+ * Creates GET and HEAD handlers for proxying authenticated Moodle media files.
62
+ *
63
+ * Mount these at a route like `app/api/moodle-media/route.ts`:
64
+ * ```ts
65
+ * import { createMoodleMediaHandler } from '@moodle-next/next';
66
+ * import { getSession } from '@/lib/session';
67
+ * export const { GET, HEAD } = createMoodleMediaHandler(getSession);
68
+ * ```
69
+ */
70
+ declare function createMoodleMediaHandler(getSession: GetSession$1, onAuthFailure?: OnAuthFailure$1): {
71
+ GET: (request: Request) => Promise<Response>;
72
+ HEAD: (request: Request) => Promise<Response>;
73
+ };
74
+
75
+ type GetSession = () => Promise<{
76
+ token: string;
77
+ } | null>;
78
+ type OnAuthFailure = () => Promise<Response> | Response;
79
+ /**
80
+ * Creates GET and HEAD handlers for proxying authenticated Moodle SCORM packages.
81
+ *
82
+ * Mount these at a route like `app/api/moodle-scorm/[base]/[[...path]]/route.ts`:
83
+ * ```ts
84
+ * import { createMoodleScormHandler } from '@moodle-next/next';
85
+ * import { getSession } from '@/lib/session';
86
+ * export const { GET, HEAD } = createMoodleScormHandler(getSession);
87
+ * ```
88
+ *
89
+ * The optional `injectIntoHtml` callback lets you intercept HTML responses and
90
+ * inject a SCORM runtime shim before the content is returned to the browser.
91
+ */
92
+ declare function createMoodleScormHandler(getSession: GetSession, options?: {
93
+ onAuthFailure?: OnAuthFailure;
94
+ /** Called when the upstream response is an HTML document. Return modified HTML. */
95
+ injectIntoHtml?: (html: string, requestUrl: URL) => string | Promise<string>;
96
+ }): {
97
+ GET: (request: Request, context: {
98
+ params: Promise<{
99
+ base: string;
100
+ path?: string[];
101
+ }>;
102
+ }) => Promise<Response>;
103
+ HEAD: (request: Request, context: {
104
+ params: Promise<{
105
+ base: string;
106
+ path?: string[];
107
+ }>;
108
+ }) => Promise<Response>;
109
+ };
110
+
111
+ /**
112
+ * Creates GET and HEAD handlers for serving the Moodle site logo (with SVG fallback).
113
+ *
114
+ * Mount these at a route like `app/api/moodle-brand-logo/route.ts`:
115
+ * ```ts
116
+ * import { createMoodleBrandLogoHandler } from '@moodle-next/next';
117
+ * export const { GET, HEAD } = createMoodleBrandLogoHandler();
118
+ * ```
119
+ *
120
+ * Accepts `?variant=full` (default) or `?variant=compact`.
121
+ */
122
+ declare function createMoodleBrandLogoHandler(): {
123
+ GET: (request: Request) => Promise<Response>;
124
+ HEAD: (request: Request) => Promise<Response>;
125
+ };
126
+
127
+ /**
128
+ * Returns a URL for the media proxy route handler.
129
+ * Mount `createMoodleMediaHandler()` at `basePath` in your Next.js app.
130
+ */
131
+ declare function getMoodleMediaProxyUrl(rawUrl?: string, basePath?: string): string | undefined;
132
+ /**
133
+ * Returns a URL for the SCORM proxy route handler.
134
+ * Mount `createMoodleScormHandler()` at `basePath/[base]/[[...path]]` in your Next.js app.
135
+ */
136
+ declare function getMoodleScormProxyUrl(rawUrl?: string, basePath?: string): string | undefined;
137
+ /**
138
+ * Returns a URL for the brand logo proxy route handler.
139
+ * Mount `createMoodleBrandLogoHandler()` at `basePath` in your Next.js app.
140
+ */
141
+ declare function getMoodleBrandLogoProxyUrl(variant?: 'full' | 'compact', basePath?: string): string;
142
+
143
+ /**
144
+ * Validates that a return path is a relative URL within an allowed section
145
+ * to prevent open redirect attacks.
146
+ *
147
+ * @param rawPath - The raw path to validate (typically from a query param).
148
+ * @param fallback - The fallback path if `rawPath` is invalid.
149
+ * @param allowedPrefixes - Path prefixes to accept. Defaults to `["/"]` (any relative path).
150
+ */
151
+ declare function sanitizeReturnPath(rawPath: string | null | undefined, fallback: string, allowedPrefixes?: string[]): string;
152
+
153
+ export { type MoodleBranding, type MoodleSession, clearSession, clearSessionAndReturnUnauthorized, clearSessionIfAuthenticationError, configureMoodleSession, createMoodleBrandLogoHandler, createMoodleMediaHandler, createMoodleScormHandler, createSession, getMoodleBrandLogoProxyUrl, getMoodleBranding, getMoodleMediaProxyUrl, getMoodleScormProxyUrl, getSession, getSessionOrUnauthorizedResponse, getSiteForceLogin, getSiteGlobalSearchEnabled, getSiteMessagingEnabled, redirectIfSessionExpired, redirectToExpiredSession, requireSession, sanitizeReturnPath };
@@ -0,0 +1,153 @@
1
+ type MoodleSession = {
2
+ token: string;
3
+ userId: number;
4
+ username: string;
5
+ fullName: string;
6
+ email?: string;
7
+ userPictureUrl?: string;
8
+ canManagePlatform?: boolean;
9
+ canManageOwnFiles?: boolean;
10
+ expiresAt: number;
11
+ };
12
+ type MoodleSessionConfig = {
13
+ /** Cookie name. Default: `"moodle_session"` */
14
+ cookieName?: string;
15
+ /** Session lifetime in seconds. Default: `28800` (8 hours) */
16
+ durationSeconds?: number;
17
+ /** Path to redirect unauthenticated requests to. Default: `"/"` */
18
+ loginPath?: string;
19
+ /** Path to redirect when the Moodle token has expired. Default: `"/auth/session-expired"` */
20
+ expiredSessionPath?: string;
21
+ /**
22
+ * Session encryption secret. Falls back to `process.env.APP_SESSION_SECRET`.
23
+ * Must be set before calling any session function.
24
+ */
25
+ sessionSecret?: string;
26
+ };
27
+ declare function configureMoodleSession(config: MoodleSessionConfig): void;
28
+ declare function createSession(session: Omit<MoodleSession, 'expiresAt'>): Promise<void>;
29
+ declare function getSession(): Promise<MoodleSession | null>;
30
+ declare function requireSession(): Promise<MoodleSession>;
31
+ declare function clearSession(): Promise<void>;
32
+ declare function redirectToExpiredSession(): never;
33
+ declare function redirectIfSessionExpired(error: unknown): void;
34
+ declare function clearSessionIfAuthenticationError(error: unknown): Promise<boolean>;
35
+ declare function getSessionOrUnauthorizedResponse(): Promise<{
36
+ session: MoodleSession;
37
+ response: null;
38
+ } | {
39
+ session: null;
40
+ response: Response;
41
+ }>;
42
+ declare function clearSessionAndReturnUnauthorized(): Promise<Response>;
43
+
44
+ type MoodleBranding = {
45
+ siteName: string;
46
+ siteDescription?: string;
47
+ siteUrl: string;
48
+ logoUrl?: string;
49
+ compactLogoUrl?: string;
50
+ };
51
+ declare const getMoodleBranding: () => Promise<MoodleBranding>;
52
+ declare function getSiteForceLogin(): Promise<boolean>;
53
+ declare function getSiteGlobalSearchEnabled(): Promise<boolean>;
54
+ declare function getSiteMessagingEnabled(): Promise<boolean>;
55
+
56
+ type GetSession$1 = () => Promise<{
57
+ token: string;
58
+ } | null>;
59
+ type OnAuthFailure$1 = () => Promise<Response> | Response;
60
+ /**
61
+ * Creates GET and HEAD handlers for proxying authenticated Moodle media files.
62
+ *
63
+ * Mount these at a route like `app/api/moodle-media/route.ts`:
64
+ * ```ts
65
+ * import { createMoodleMediaHandler } from '@moodle-next/next';
66
+ * import { getSession } from '@/lib/session';
67
+ * export const { GET, HEAD } = createMoodleMediaHandler(getSession);
68
+ * ```
69
+ */
70
+ declare function createMoodleMediaHandler(getSession: GetSession$1, onAuthFailure?: OnAuthFailure$1): {
71
+ GET: (request: Request) => Promise<Response>;
72
+ HEAD: (request: Request) => Promise<Response>;
73
+ };
74
+
75
+ type GetSession = () => Promise<{
76
+ token: string;
77
+ } | null>;
78
+ type OnAuthFailure = () => Promise<Response> | Response;
79
+ /**
80
+ * Creates GET and HEAD handlers for proxying authenticated Moodle SCORM packages.
81
+ *
82
+ * Mount these at a route like `app/api/moodle-scorm/[base]/[[...path]]/route.ts`:
83
+ * ```ts
84
+ * import { createMoodleScormHandler } from '@moodle-next/next';
85
+ * import { getSession } from '@/lib/session';
86
+ * export const { GET, HEAD } = createMoodleScormHandler(getSession);
87
+ * ```
88
+ *
89
+ * The optional `injectIntoHtml` callback lets you intercept HTML responses and
90
+ * inject a SCORM runtime shim before the content is returned to the browser.
91
+ */
92
+ declare function createMoodleScormHandler(getSession: GetSession, options?: {
93
+ onAuthFailure?: OnAuthFailure;
94
+ /** Called when the upstream response is an HTML document. Return modified HTML. */
95
+ injectIntoHtml?: (html: string, requestUrl: URL) => string | Promise<string>;
96
+ }): {
97
+ GET: (request: Request, context: {
98
+ params: Promise<{
99
+ base: string;
100
+ path?: string[];
101
+ }>;
102
+ }) => Promise<Response>;
103
+ HEAD: (request: Request, context: {
104
+ params: Promise<{
105
+ base: string;
106
+ path?: string[];
107
+ }>;
108
+ }) => Promise<Response>;
109
+ };
110
+
111
+ /**
112
+ * Creates GET and HEAD handlers for serving the Moodle site logo (with SVG fallback).
113
+ *
114
+ * Mount these at a route like `app/api/moodle-brand-logo/route.ts`:
115
+ * ```ts
116
+ * import { createMoodleBrandLogoHandler } from '@moodle-next/next';
117
+ * export const { GET, HEAD } = createMoodleBrandLogoHandler();
118
+ * ```
119
+ *
120
+ * Accepts `?variant=full` (default) or `?variant=compact`.
121
+ */
122
+ declare function createMoodleBrandLogoHandler(): {
123
+ GET: (request: Request) => Promise<Response>;
124
+ HEAD: (request: Request) => Promise<Response>;
125
+ };
126
+
127
+ /**
128
+ * Returns a URL for the media proxy route handler.
129
+ * Mount `createMoodleMediaHandler()` at `basePath` in your Next.js app.
130
+ */
131
+ declare function getMoodleMediaProxyUrl(rawUrl?: string, basePath?: string): string | undefined;
132
+ /**
133
+ * Returns a URL for the SCORM proxy route handler.
134
+ * Mount `createMoodleScormHandler()` at `basePath/[base]/[[...path]]` in your Next.js app.
135
+ */
136
+ declare function getMoodleScormProxyUrl(rawUrl?: string, basePath?: string): string | undefined;
137
+ /**
138
+ * Returns a URL for the brand logo proxy route handler.
139
+ * Mount `createMoodleBrandLogoHandler()` at `basePath` in your Next.js app.
140
+ */
141
+ declare function getMoodleBrandLogoProxyUrl(variant?: 'full' | 'compact', basePath?: string): string;
142
+
143
+ /**
144
+ * Validates that a return path is a relative URL within an allowed section
145
+ * to prevent open redirect attacks.
146
+ *
147
+ * @param rawPath - The raw path to validate (typically from a query param).
148
+ * @param fallback - The fallback path if `rawPath` is invalid.
149
+ * @param allowedPrefixes - Path prefixes to accept. Defaults to `["/"]` (any relative path).
150
+ */
151
+ declare function sanitizeReturnPath(rawPath: string | null | undefined, fallback: string, allowedPrefixes?: string[]): string;
152
+
153
+ export { type MoodleBranding, type MoodleSession, clearSession, clearSessionAndReturnUnauthorized, clearSessionIfAuthenticationError, configureMoodleSession, createMoodleBrandLogoHandler, createMoodleMediaHandler, createMoodleScormHandler, createSession, getMoodleBrandLogoProxyUrl, getMoodleBranding, getMoodleMediaProxyUrl, getMoodleScormProxyUrl, getSession, getSessionOrUnauthorizedResponse, getSiteForceLogin, getSiteGlobalSearchEnabled, getSiteMessagingEnabled, redirectIfSessionExpired, redirectToExpiredSession, requireSession, sanitizeReturnPath };