UNPKG

scormcloud-client

Version:

A Typescript and JS client for interfacing with the ScormCloud API

366 lines (365 loc) 22 kB
import { AuthToken, HttpError, ErrorObject, ErrorProperty, Options, Course, Learner, LaunchLink, Registration, PingResponse, ImportJobResult, SuccessIndicator, CourseFetchOptions, CourseQueryOptions, CourseQueryResponse, CourseImportOptions, CourseImportResponse, LaunchLinkOptions, RegistrationOptions, RegistrationFetchOptions, RegistrationQueryOptions, RegistrationQueryResponse, CourseVersionFetchOptions, CourseVersionAssetUploadOptions } from './types'; /** @internal */ export declare const TypeChecks: { containsErrorProperty: (x: any) => x is ErrorProperty; isErrorObject: (x: any) => x is ErrorObject; isAuthToken: (x: any) => x is AuthToken; isHttpError: (x: any) => x is HttpError; }; /** @internal */ export declare const Util: { hasProperty<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown>; getOption: (name: string, options?: Partial<Options>) => any; sleep: (milliseconds: number) => unknown; scormUploadType: (f: string) => string; }; export declare class ScormClientError extends Error { httpStatus: number | undefined; cause: ErrorObject | undefined; /** @internal */ constructor(cause: any, message?: string, httpStatus?: number); private static parseMessage; private static parseHttpStatus; private static parseErrorObject; private static parse; } /** * * Usage example: * * ```ts * import { ScormClient } from 'scorm-client' * * const client = new ScormClient(appId, secretKey, "read") * * // will fetch a course using a token with the default scope, in this case 'read' * const course: Course = await client.getCourse(courseId) * * // will delete a course using a token with 'write' scope * const result: SuccessIndicator = await client.deleteCourse(courseId, { scope: 'write' }) * ``` * * You will notice that no call has been made to the `authenticate()` method before starting to use the client. * When a method is invoked and no valid auth token for the given scope can be found, then the client will attempt * to authenticate and fetch a token for that scope. As such, tokens are explicitly associated with a specific scope, * and the client will internally manage a set of tokens and their associated scopes. * * Any future calls for a given scope will use the token associated with it. If no scope is specified (in the * `options` for a method), then the default scope (assigned at client instantiation) will be assumed. You are able * to manually `authenticate()` different scopes, which is simply asking the client to fetch and store a token for * a scope, such that it is immediately available in method calls later. * * Note: the scope can also be a list of space separated scopes, e.g. "write:course read:registration". In such a * case the token will be associated with all the specified scopes. */ export declare class ScormClient { private readonly appId; private readonly secretKey; private readonly defaultScope; private readonly defaultExpiry; private readonly authorisations; /** * @param appId A SCORM Cloud application ID * @param secretKey The secret key for the given application ID * @param defaultScope An auth scope or space separated list of scopes, e.g. "write:course read:registration". * This will be the default scope used if a method on the client is invoked without specifying an explicit * scope in the method's {@link types.Options} * @param defaultExpiry The amount of time, in seconds, after which auth tokens should expire. If unspecified, * it will default to 3600 (1 hour) */ constructor(appId: string, secretKey: string, defaultScope: string, defaultExpiry?: number); private getTargetScope; private getAuthToken; private getBearerString; private authorise; /** * Attempt to authenticate and store an auth token, associated with a given scope * * @param scope An auth scope or space separated list of scopes * @param expiry The amount of time, in seconds, after which the auth token should expire. If unspecified, * it will use the default value provided in the constructor. * @throws {@link ScormClientError} with an `httpStatus` of 401 on authentication failure */ authenticate(scope: string, expiry?: number): Promise<AuthToken>; /** * Get back a message indicating that the API is working * * [API Method - PingAppId](https://cloud.scorm.com/docs/v2/reference/swagger/#/ping/PingAppId) * * @param options Options * @throws {@link ScormClientError} if invalid ping response received, or an error was encountered */ ping(options?: Options): Promise<PingResponse>; /** * Returns detailed information about the course. * * [API Method - GetCourse](https://cloud.scorm.com/docs/v2/reference/swagger/#/course/GetCourse) * * @param courseId The course ID * @param options CourseFetchOptions * @throws {@link ScormClientError} if an invalid request was made, or an error encountered * @returns The requested {@link types.Course}, or `undefined` if the course was not found */ getCourse(courseId: string, options?: CourseFetchOptions): Promise<Course | undefined>; /** * Returns a list of courses. Can be filtered to provide a subset of results. * * This request is paginated and will only provide a limited amount of resources at a time. If there are more * results to be collected, a more token provided with the response which can be passed to get the next page * of results. When passing this token, no other filter parameters can be sent as part of the request. The * resources will continue to respect the filters passed in by the original request. * [API Method - GetCourses](https://cloud.scorm.com/docs/v2/reference/swagger/#/course/GetCourses) * * @param options CourseQueryOptions * @throws {@link ScormClientError} if an invalid request was made, or an error encountered * @returns A {@link types.CourseQueryResponse} that has a property named `courses`, which is an array * of type {@link types.Course}, or an empty array if no courses were found. It also includes a * '`more`' property which, if present, can be used to retrieve the next paginated set of results */ getCourses(options?: CourseQueryOptions): Promise<CourseQueryResponse>; /** * Creates a course from a package uploaded from your file system. The package will be sent as part of the request * and will be stored in SCORM Cloud. An import job ID will be returned, which can be used with {@link getCourseImportStatus} * to view the status of the import. Courses represent the learning material a learner will progress through. * Note: The import job ID is only valid for one week after the course import finishes. * * [API Method - CreateUploadAndImportCourseJob](https://cloud.scorm.com/docs/v2/reference/swagger/#/course/CreateUploadAndImportCourseJob) * * @param courseId The course ID * @param filePath The path to the file that must be imported to SCORM Cloud * @param options CourseImportOptions * @throws {@link ScormClientError} if an invalid request was made, or an error encountered * @returns A {@link types.CourseImportResponse} if successfull */ importCourse(courseId: string, filePath: string, options?: CourseImportOptions): Promise<CourseImportResponse>; /** * Check the status of a course import. This can be called incrementally to check the progress of a call to any * of the import options. Note: The import job ID is only valid for one week after the course import finishes. * * [API Method - GetImportJobStatus](https://cloud.scorm.com/docs/v2/reference/swagger/#/course/GetImportJobStatus) * * @param jobId The import job ID * @param options Options * @throws {@link ScormClientError} if an invalid request was made, or an error encountered * @returns A {@link types.ImportJobResult}, or `undefined` if no job result was not found */ getCourseImportStatus(jobId: string, options?: Options): Promise<ImportJobResult | undefined>; /** * Updates the title of the course. * * [API Method - SetCourseTitle](https://cloud.scorm.com/docs/v2/reference/swagger/#/course/SetCourseTitle) * * @param courseId The course ID * @param title The new title to be set * @param options Options * @throws {@link ScormClientError} if an invalid request was made, or an error encountered, * or if the referenced course does not exist * @returns A {@link types.SuccessIndicator} */ setCourseTitle(courseId: string, title: string, options?: Options): Promise<SuccessIndicator>; /** * Deletes the specified course. When a course is deleted, so is everything connected to the course. This includes: * - Registrations * - Invitations * - Dispatches * - Debug Logs * * [API Method - DeleteCourse](https://cloud.scorm.com/docs/v2/reference/swagger/#/course/DeleteCourse) * * @param courseId The course ID * @param options Options * @throws {@link ScormClientError} if an invalid request was made, or an error encountered, * or if the referenced course does not exist * @returns A {@link types.SuccessIndicator} */ deleteCourse(courseId: string, options?: Options): Promise<SuccessIndicator>; /** * TODO: Test this function to see what happens with non-existent courses and/or course versions and confirm what * is return and what is thrown. Update the function to handle scenarios appropriately ... * * Returns information about all versions of the course. This can be useful to see information such as registration * counts and modification times across the versions of a course. * * [API Method - GetCourseVersions](https://cloud.scorm.com/docs/v2/reference/swagger/#/course/GetCourseVersions) * * @param courseId The course ID * @param options CourseVersionFetchOptions * @throws {@link ScormClientError} if an invalid request was made, or an error encountered, * @returns An array of {@link types.Course}, or an empty array if no courses were found. This method does not * support pagination, all versions will be returned */ getCourseVersions(courseId: string, options?: CourseVersionFetchOptions): Promise<Course[] | undefined>; /** * Deletes the specified version of the course. If deleting the last remaining version of the course, * the course itself will be deleted and no longer accessible. When a course is deleted, so is everything * connected to the course. This includes: * - Registrations * - Invitations * - Dispatches * - Debug Logs * * [API Method - DeleteCourseVersion](https://cloud.scorm.com/docs/v2/reference/swagger/#/course/DeleteCourseVersion) * * @param courseId The course ID * @param versionNumber The version number * @param options Options * @throws {@link ScormClientError} if an invalid request was made, or an error encountered, * or if the referenced course does not exist * @returns A {@link types.SuccessIndicator} */ deleteCourseVersion(courseId: string, versionNumber: number, options?: Options): Promise<SuccessIndicator>; /** * Creates or updates an asset file uploaded from your file system into the course version. The file will be sent * as part of the request and will be stored in SCORM Cloud alongside the course. This is a useful way to modify * the course structure without needing to reimport the whole course after you've made changes. * * If the course structure is being heavily modified, consider creating a new version instead. This can be done by * calling {@link importCourse}, while passing true for mayCreateNewVersion * * [API Method - UploadCourseVersionAssetFile](https://cloud.scorm.com/docs/v2/reference/swagger/#/course/UploadCourseVersionAssetFile) * * @param courseId The course ID * @param versionNumber The version number * @param filePath The local path to the file that must be imported to SCORM Cloud * @param destinationPath The relative path from the course's base directory on SCORM Cloud, to where the asset file will be uploaded * @param options CourseVersionAssetUploadOptions * @throws {@link ScormClientError} if an invalid request was made, or an error encountered * @returns A {@link types.CourseVersionAssetUploadResponse} if successfull */ uploadCourseVersionAssetFile(courseId: string, versionNumber: number, filePath: string, destinationPath: string, options?: CourseVersionAssetUploadOptions): Promise<SuccessIndicator>; /** * Creates a new registration. Registrations are the billable unit in SCORM Cloud, and represents the link between * a learner and a course. A registration will contain a few pieces of information such as learner identifiers, * the id of the course being registered for, and several other optional fields. * * A registration must be tied to a specific course at creation time. When the registration is "launched" * (see {@link createLaunchLink}), the course specified at creation time will be launched. * * [API Method - CreateRegistration](https://cloud.scorm.com/docs/v2/reference/swagger/#/registration/CreateRegistration) * * @param registrationId An ID for this registration * @param courseId The course ID for which the learner should be registered * @param learner The details of the learner, at minimum a learner ID should be specified * @param options RegistrationOptions * @throws {@link ScormClientError} if an invalid request was made, or an error encountered, * or if the referenced course or learner does not exist, or if creating a duplicate registration * @returns A {@link types.SuccessIndicator} */ createRegistration(registrationId: string, courseId: string, learner: Learner, options?: RegistrationOptions): Promise<SuccessIndicator>; /** * Checks that the registration exists within SCORM Cloud. No registration data will be returned for this call. * If you are looking for information about the registration, try using {@link getRegistration} instead. * * [API Method - GetRegistration](https://cloud.scorm.com/docs/v2/reference/swagger/#/registration/GetRegistration) * * @param registrationId An ID for which registration to check * @param options Options * @throws {@link ScormClientError} if an invalid request was made * @returns A boolean true or false */ registrationExists(registrationId: string, options?: Options): Promise<Boolean>; /** * Deletes the specified registration. This will also delete all instances of the registration. * * [API Method - DeleteRegistration](https://cloud.scorm.com/docs/v2/reference/swagger/#/registration/DeleteRegistration) * * @param registrationId The registration ID * @param options Options * @throws {@link ScormClientError} if an invalid request was made, or an error encountered, * or if the referenced registration does not exist * @returns A {@link types.SuccessIndicator} */ deleteRegistration(registrationId: string, options?: Options): Promise<SuccessIndicator>; /** * Returns detailed information about the registration. This includes completion status, time taken, score, * and pass/fail status. * * [API Method - GetRegistrationProgress](https://cloud.scorm.com/docs/v2/reference/swagger/#/registration/GetRegistrationProgress) * * @param registrationId The registration ID for which to return progress * @param options RegistrationFetchOptions */ getRegistration(registrationId: string, options?: RegistrationFetchOptions): Promise<Registration>; /** * This is an overloaded method for {@link getRegistration}, and is exactly the same thing. It exists purely to provide a * method named in similar fashion to the official API. * * [API Method - GetRegistrationProgress](https://cloud.scorm.com/docs/v2/reference/swagger/#/registration/GetRegistrationProgress) * * @param registrationId The registration ID for which to return progress * @param options RegistrationFetchOptions */ getRegistrationProgress(registrationId: string, options?: RegistrationFetchOptions): Promise<Registration>; /** * Returns a list of registrations for the given course. Can be filtered to provide a subset of results. * * This request is paginated and will only provide a limited amount of resources at a time. If there are more * results to be collected, a more token provided with the response which can be passed to get the next page * of results. When passing this token, no other filter parameters can be sent as part of the request. The * resources will continue to respect the filters passed in by the original request. * * [API Method - GetRegistrations](https://cloud.scorm.com/docs/v2/reference/swagger/#/registration/GetRegistrations) * * @param courseId The course ID for which to return registrations * @param options RegistrationQueryOptions * @throws {@link ScormClientError} if an invalid request was made, or an error encountered * @returns A {@link types.RegistrationQueryResponse} that has a property named `registrations`, which is an array * of type {@link types.Registration}, or an empty array if no registrations were found. It also includes a * '`more`' property which, if present, can be used to retrieve the next paginated set of results */ getRegistrationsForCourse(courseId: string, options?: RegistrationQueryOptions): Promise<RegistrationQueryResponse>; /** * Returns a list of registrations for the given learner. Can be filtered to provide a subset of results. * * This request is paginated and will only provide a limited amount of resources at a time. If there are more * results to be collected, a more token provided with the response which can be passed to get the next page * of results. When passing this token, no other filter parameters can be sent as part of the request. The * resources will continue to respect the filters passed in by the original request. * * [API Method - GetRegistrations](https://cloud.scorm.com/docs/v2/reference/swagger/#/registration/GetRegistrations) * * @param learnerId The learner ID for which to return registrations * @param options RegistrationQueryOptions * @throws {@link ScormClientError} if an invalid request was made, or an error encountered * @returns A {@link types.RegistrationQueryResponse} that has a property named `registrations`, which is an array * of type {@link types.Registration}, or an empty array if no registrations were found. It also includes a * '`more`' property which, if present, can be used to retrieve the next paginated set of results */ getRegistrationsForLearner(learnerId: string, options?: RegistrationQueryOptions): Promise<RegistrationQueryResponse>; /** * Returns a list of registrations. Can be filtered to provide a subset of results. * * This request is paginated and will only provide a limited amount of resources at a time. If there are more * results to be collected, a more token provided with the response which can be passed to get the next page * of results. When passing this token, no other filter parameters can be sent as part of the request. The * resources will continue to respect the filters passed in by the original request. * * [API Method - GetRegistrations](https://cloud.scorm.com/docs/v2/reference/swagger/#/registration/GetRegistrations) * * @param options RegistrationQueryOptions * @throws {@link ScormClientError} if an invalid request was made, or an error encountered * @returns A {@link types.RegistrationQueryResponse} that has a property named `registrations`, which is an array * of type {@link types.Registration}, or an empty array if no registrations were found. It also includes a * '`more`' property which, if present, can be used to retrieve the next paginated set of results */ getRegistrations(options?: RegistrationQueryOptions): Promise<RegistrationQueryResponse>; /** * Get a launch link, which is a relatively short lived url, used to launch the course for a given registration. * Launch links are meant as a way to provide access to your content. When a learner visits the link, the course * will be launched and registration progress will start to be tracked. * * [API Method - BuildRegistrationLaunchLink](https://cloud.scorm.com/docs/v2/reference/swagger/#/registration/BuildRegistrationLaunchLink) * * @param registrationId The registration ID for which to create and return a launch link * @param redirectOnExitUrl The URL the application should redirect to when the learner exits or completes a course. * Alternatively one of the following keywords can be used: * - closer : A page that automatically tries to close the browser tab/window * - blank : A blank page * - message : A page with a message about the course being complete * * If an invalid url is specified, the Message.html page will be loaded * @param options LaunchLinkOptions */ createLaunchLink(registrationId: string, redirectOnExitUrl: string, options?: LaunchLinkOptions): Promise<LaunchLink>; } //# sourceMappingURL=client.d.ts.map