import { ExperimentResource } from './experiment-schemas.ts'; import { EmptyDataDocument, getDataDocumentSchema, getErrorDocumentSchema, getErrorSchema, getResourceIdentifierSchema, } from './jsonapi.ts'; import { LogResource } from './log-schemas.ts'; import { RunResource, RunResourceIdentifier } from './run-schemas.ts'; import { sessionAuth } from './security.ts'; import { mediaType, userRoles } from './vocabulary.ts'; import { registry, type RouteConfig, z } from './zod-openapi.ts'; const UserRole = z.enum(userRoles); // Resource schema // ----------------------------------------------------------------------------- export const SessionResourceIdentifier = getResourceIdentifierSchema( 'sessions', ).openapi('SessionResourceIdentifier'); const SessionAttributes = z.strictObject({ role: UserRole.optional() }); const SessionRelationships = z.strictObject({ runs: z.strictObject({ data: z.array(RunResourceIdentifier) }), }); export const SessionResource = z .strictObject({ ...SessionResourceIdentifier.shape, attributes: SessionAttributes, relationships: SessionRelationships, }) .openapi('SessionResource'); const SessionResourceCreate = SessionResource.omit({ id: true, relationships: true, }); // Request schemas // ----------------------------------------------------------------------------- const SessionPostRequest = getDataDocumentSchema({ data: SessionResourceCreate, }).openapi('SessionPostRequest'); // Response schemas // ----------------------------------------------------------------------------- const SessionPostResponse = getDataDocumentSchema({ data: SessionResource, }).openapi('SessionPostResponse'); const SessionGetResponse = getDataDocumentSchema({ data: SessionResource, includes: z.union([RunResource, LogResource, ExperimentResource]), }).openapi('SessionGetResponse'); // ----------------------------------------------------------------------------- const SessionIncludeName = z.enum(['runs', 'runs.experiment', 'runs.lastLogs']); const IncludesQuery = z .union([SessionIncludeName, z.array(SessionIncludeName)]) .optional() .describe('Related resources to include in the response'); // Error schemas // ----------------------------------------------------------------------------- const SessionNotFoundErrorResponse = getErrorDocumentSchema( getErrorSchema({ code: 'SESSION_NOT_FOUND', statusCode: 404 }), ).openapi('SessionNotFoundErrorResponse'); const SessionExistsErrorResponse = getErrorDocumentSchema( getErrorSchema({ code: 'SESSION_EXISTS', statusCode: 409 }), ).openapi('SessionExistsErrorResponse'); const InvalidCredentialErrorResponse = getErrorDocumentSchema( getErrorSchema({ code: ['INVALID_CREDENTIALS', 'MISSING_CREDENTIALS'], statusCode: 403, }), ).openapi('InvalidCredentialErrorResponse'); // Common answers // ----------------------------------------------------------------------------- const sessionNotFoundResponse = { 404: { description: 'Session not found or missing credentials', content: { [mediaType]: { schema: SessionNotFoundErrorResponse } }, }, }; // Authentication // ----------------------------------------------------------------------------- const basicAuth = registry.registerComponent('securitySchemes', 'BasicAuth', { type: 'http', scheme: 'Basic', }); // Route configuration // ----------------------------------------------------------------------------- export const sessionRoutes = { '/': { post: { description: 'Create a new session', security: [{}, { [basicAuth.name]: [] }], request: { body: { required: true, content: { [mediaType]: { schema: SessionPostRequest } }, }, headers: z.looseObject({ authorization: z .string() .optional() .describe( 'Basic authentication header for host role. Format: "Basic base64(username:password)"', ), }), }, responses: { 201: { description: 'Session created successfully. If necessary, login and password are provided using the basic authentication scheme. The session ID is returned in a cookie to be used with cookie authentication.', content: { [mediaType]: { schema: SessionPostResponse } }, headers: z.strictObject({ location: z .string() .describe('The URL of the created session resource'), 'set-cookie': z .string() .describe('Session cookie for the created session'), }), }, 403: { description: 'Invalid credentials provided', content: { [mediaType]: { schema: InvalidCredentialErrorResponse } }, }, 409: { description: 'Client already has an active session', content: { [mediaType]: { schema: SessionExistsErrorResponse } }, }, }, }, }, '/{id}': { get: { security: [{}, { [sessionAuth.name]: [] }], description: 'Get a session by ID', request: { params: z.strictObject({ id: z.string().describe('ID of the session to retrieve'), }), query: z.strictObject({ include: IncludesQuery }), }, responses: { 200: { description: 'Session retrieved successfully', content: { [mediaType]: { schema: SessionGetResponse } }, }, ...sessionNotFoundResponse, }, }, delete: { security: [{}, { [basicAuth.name]: [] }], description: 'Delete a session by ID', request: { params: z.strictObject({ id: z.string().describe('ID of the session to remove'), }), }, responses: { 200: { description: 'Session deleted successfully', content: { [mediaType]: { schema: EmptyDataDocument } }, }, ...sessionNotFoundResponse, }, }, }, } satisfies RouteConfig;