From 58981b8b377c300c1f48350bbd6e7d7c0e0ddb4f Mon Sep 17 00:00:00 2001 From: Sergei Bronnikov Date: Mon, 27 Oct 2025 14:46:16 +0000 Subject: [PATCH 1/5] create course module assignment --- package.json | 2 +- src/lib/assignment.ts | 46 +++++++++++++++++++++++++++++++++++++++- src/lib/course.ts | 49 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index bb40f76..129fdb8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "codio-api-js", - "version": "0.15.3", + "version": "0.16.0", "description": "JS client to Codio API", "repository": "https://github.com/codio/codio-api-js", "author": "Max Kraev ", diff --git a/src/lib/assignment.ts b/src/lib/assignment.ts index 9b34ad6..688463b 100644 --- a/src/lib/assignment.ts +++ b/src/lib/assignment.ts @@ -447,6 +447,49 @@ export async function updateStudentTimeExtension( } } +export type ProjectSource = { + type: 'git', url: string, credentials?: {username: string, password: string} +} | { + type: 'zip' +} + +export type AssignmentData = { + unitId: string + settings: { + name: string + description?: string + imageUrl?: string + gigaboxSlot?: { + boxType: string + } + } + projectSource: + | { type: 'empty', stackVersionId: string } + | { type: 'existing', projectId: string } + | { type: 'starterPack', id: string } + | { type: 'import', stackVersionId: string, source: object } +} + +export async function createAssignment(courseId: string, assignmentData: any): Promise { + const api = bent(getApiV1Url(), 'POST', 'json', 200) + try { + const res = await api(`/courses/${courseId}/assignments`, assignmentData, getBearer()) + const assignmentId = res['id'] + if (!assignmentId) { + throw new Error('assignmentId not found in response') + } + return assignmentId + } catch (error: any) { + if (error.json) { + const message = JSON.stringify(await error.json()) + console.log(message) + throw new Error(message) + } + console.log(error) + throw error + } +} + const assignment = { publish: async (courseId: string, assignmentId: string, projectPath: string, changelogOrOptions: string | PublishOptions): Promise => { @@ -459,7 +502,8 @@ const assignment = { reducePublish, updateSettings, getSettings, - updateStudentTimeExtension + updateStudentTimeExtension, + createAssignment } export default assignment diff --git a/src/lib/course.ts b/src/lib/course.ts index 97dd306..fef4b43 100644 --- a/src/lib/course.ts +++ b/src/lib/course.ts @@ -580,6 +580,51 @@ export async function filterLearnersForMentors(courseId: string, mapping: Filter } } +export type CreateCourseRequest = { + name: string, + description?: string, + start?: string, + end?: string, + timezone?: string, + tags?: string[] +} + +export async function createCourse(courseData: CreateCourseRequest): Promise { + const api = bent(getApiV1Url(), 'POST', 'json', 200) + try { + const res = await api(`/courses`, courseData, getBearer()) + const courseId = res['id'] + if (!courseId) { + throw new Error('courseId not found in response') + } + return courseId + } catch (error: any) { + if (error.json) { + const message = JSON.stringify(await error.json()) + throw new Error(message) + } + throw error + } +} + +export async function createModule(courseId: string, moduleName: string): Promise { + const api = bent(getApiV1Url(), 'POST', 'json', 200) + try { + const res = await api(`/courses/${courseId}/modules`, { name: moduleName }, getBearer()) + const moduleId = res['id'] + if (!moduleId) { + throw new Error('moduleId not found in response') + } + return moduleId + } catch (error: any) { + if (error.json) { + const message = JSON.stringify(await error.json()) + throw new Error(message) + } + throw error + } +} + const course = { assignmentStudentsProgress, info, @@ -607,7 +652,9 @@ const course = { archive, exportCoachData, exportLLMProxyData, - filterLearnersForMentors + filterLearnersForMentors, + createCourse, + createModule } export default course From 437d0b15469dcdd3fc3075ef601b1873e4a1a301 Mon Sep 17 00:00:00 2001 From: Sergei Bronnikov Date: Tue, 28 Oct 2025 11:14:37 +0000 Subject: [PATCH 2/5] examples --- examples/assignment/createAssignment.js | 11 ++++++++++ examples/course/createCourse.js | 11 ++++++++++ examples/course/createModule.js | 11 ++++++++++ examples/data.js | 28 ++++++++++++++++++++++++- src/lib/assignment.ts | 2 +- 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 examples/assignment/createAssignment.js create mode 100644 examples/course/createCourse.js create mode 100644 examples/course/createModule.js diff --git a/examples/assignment/createAssignment.js b/examples/assignment/createAssignment.js new file mode 100644 index 0000000..145bf9b --- /dev/null +++ b/examples/assignment/createAssignment.js @@ -0,0 +1,11 @@ +const { codio, auth } = require('../auth.js') +const { courseId, assignmentData } = require('../data.js') + +async function main() { + await auth + + const result = await codio.assignment.createAssignment(courseId, assignmentData) + console.log(result) +} + +main() \ No newline at end of file diff --git a/examples/course/createCourse.js b/examples/course/createCourse.js new file mode 100644 index 0000000..9a1a528 --- /dev/null +++ b/examples/course/createCourse.js @@ -0,0 +1,11 @@ +const { codio, auth } = require('../auth.js') +const { courseData } = require('../data.js') + +async function main() { + await auth + + const result = await codio.course.createCourse(courseData) + console.log(result) +} + +main() \ No newline at end of file diff --git a/examples/course/createModule.js b/examples/course/createModule.js new file mode 100644 index 0000000..14a1e11 --- /dev/null +++ b/examples/course/createModule.js @@ -0,0 +1,11 @@ +const { codio, auth } = require('../auth.js') +const { courseId, moduleName } = require('../data.js') + +async function main() { + await auth + + const result = await codio.course.createModule(courseId, moduleName) + console.log(result) +} + +main() \ No newline at end of file diff --git a/examples/data.js b/examples/data.js index 19027d1..9081ee2 100644 --- a/examples/data.js +++ b/examples/data.js @@ -21,5 +21,31 @@ const leanersMapping = [ ] } ] +const moduleName = "New Module Name" +const courseData = { + name: "Introduction to Programming", + description: "This course covers the basics of programming.", + start: "2024-08-29T09:32:55Z", + end: "2027-09-25T09:12:00Z", + timezone: "America/New_York", + tags: [ + "programming", + "beginner" + ] +} +const assignmentData = { + unitId: "yout unit id", + settings: { + name: "My First Assignment", + description: "This assignment covers the basics of programming.", + gigaboxSlot: { + boxType: "standard" + } + }, + projectSource: { + type: "starterPack", + id: "11000000-0000-0000-0000-000000000000" + } +} -module.exports = { courseId, courseName, assignmentId, studentId, studentEmail, studentLogin, libraryId, libraryName, projectPath, stackId, stackVersionId, archivePath, yamlMapDir, courseIdToArchive, leanersMapping } +module.exports = { courseId, courseName, assignmentId, studentId, studentEmail, studentLogin, libraryId, libraryName, projectPath, stackId, stackVersionId, archivePath, yamlMapDir, courseIdToArchive, leanersMapping, moduleName, courseData, assignmentData } diff --git a/src/lib/assignment.ts b/src/lib/assignment.ts index 688463b..339d387 100644 --- a/src/lib/assignment.ts +++ b/src/lib/assignment.ts @@ -470,7 +470,7 @@ export type AssignmentData = { | { type: 'import', stackVersionId: string, source: object } } -export async function createAssignment(courseId: string, assignmentData: any): Promise { +export async function createAssignment(courseId: string, assignmentData: AssignmentData): Promise { const api = bent(getApiV1Url(), 'POST', 'json', 200) try { const res = await api(`/courses/${courseId}/assignments`, assignmentData, getBearer()) From ec57f0fbaa42dbe3e6cd6193d6677bd59b9545e5 Mon Sep 17 00:00:00 2001 From: Sergei Bronnikov Date: Tue, 28 Oct 2025 11:40:36 +0000 Subject: [PATCH 3/5] empty lines --- examples/assignment/createAssignment.js | 2 +- examples/course/createCourse.js | 2 +- examples/course/createModule.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/assignment/createAssignment.js b/examples/assignment/createAssignment.js index 145bf9b..33787a5 100644 --- a/examples/assignment/createAssignment.js +++ b/examples/assignment/createAssignment.js @@ -8,4 +8,4 @@ async function main() { console.log(result) } -main() \ No newline at end of file +main() diff --git a/examples/course/createCourse.js b/examples/course/createCourse.js index 9a1a528..a90264a 100644 --- a/examples/course/createCourse.js +++ b/examples/course/createCourse.js @@ -8,4 +8,4 @@ async function main() { console.log(result) } -main() \ No newline at end of file +main() diff --git a/examples/course/createModule.js b/examples/course/createModule.js index 14a1e11..7a283a2 100644 --- a/examples/course/createModule.js +++ b/examples/course/createModule.js @@ -8,4 +8,4 @@ async function main() { console.log(result) } -main() \ No newline at end of file +main() From 0eed0bb76aa223d5b56fc36b84753bb7d82e6d53 Mon Sep 17 00:00:00 2001 From: Sergei Bronnikov Date: Tue, 28 Oct 2025 15:34:51 +0000 Subject: [PATCH 4/5] refactor --- examples/data.js | 8 ++------ src/lib/assignment.ts | 7 +------ 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/examples/data.js b/examples/data.js index 9081ee2..84dcf49 100644 --- a/examples/data.js +++ b/examples/data.js @@ -25,7 +25,7 @@ const moduleName = "New Module Name" const courseData = { name: "Introduction to Programming", description: "This course covers the basics of programming.", - start: "2024-08-29T09:32:55Z", + start: "2025-08-29T09:32:55Z", end: "2027-09-25T09:12:00Z", timezone: "America/New_York", tags: [ @@ -34,17 +34,13 @@ const courseData = { ] } const assignmentData = { - unitId: "yout unit id", + moduleId: "your module id", settings: { name: "My First Assignment", description: "This assignment covers the basics of programming.", gigaboxSlot: { boxType: "standard" } - }, - projectSource: { - type: "starterPack", - id: "11000000-0000-0000-0000-000000000000" } } diff --git a/src/lib/assignment.ts b/src/lib/assignment.ts index 339d387..43340e0 100644 --- a/src/lib/assignment.ts +++ b/src/lib/assignment.ts @@ -454,7 +454,7 @@ export type ProjectSource = { } export type AssignmentData = { - unitId: string + moduleId: string settings: { name: string description?: string @@ -463,11 +463,6 @@ export type AssignmentData = { boxType: string } } - projectSource: - | { type: 'empty', stackVersionId: string } - | { type: 'existing', projectId: string } - | { type: 'starterPack', id: string } - | { type: 'import', stackVersionId: string, source: object } } export async function createAssignment(courseId: string, assignmentData: AssignmentData): Promise { From 2116a0cb8fe2e8fd484391e39e2df654b77fc408 Mon Sep 17 00:00:00 2001 From: Sergei Bronnikov Date: Wed, 29 Oct 2025 17:37:12 +0000 Subject: [PATCH 5/5] example --- examples/data.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/data.js b/examples/data.js index 84dcf49..2131e89 100644 --- a/examples/data.js +++ b/examples/data.js @@ -39,7 +39,7 @@ const assignmentData = { name: "My First Assignment", description: "This assignment covers the basics of programming.", gigaboxSlot: { - boxType: "standard" + boxType: "1gb" } } }