0.1.5 • Published 8 months ago

getcourse-js-sdk v0.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

GetCourse JS SDK

npm dependencies license

Simple GetCourse.ru API client for JavaScript. Based on @shevernitskiy/amo

Usage

Installation

Node.JS

npm i getcourse-js-sdk

Basic example

import { FormatError, GetCourse, ServerError } from 'getcourse-js-sdk'

try {
  const gc = new GetCourse('belyaevdev.getcourse.ru', 'longapitoken')

  const res = await gc.user.addUser({
    user: {
      email: 'test@mail.ru',
    },
  })

  console.log(res)
}
catch (err) {
  if (err instanceof FormatError || err instanceof ServerError)
    console.error(err.response)
  else console.error(err)
}

Export example

import { FormatError, GetCourse, ServerError } from 'getcourse-js-sdk'
import type { ExportApiResponse, ExportedData } from 'getcourse-js-sdk'

async function checkExport(
  gc: GetCourse,
  exportId: number
): Promise<ExportApiResponse<ExportedData>> {
  const result = await gc.account.getExportResult(exportId)

  if (result.error_code && result.error_code === 909) {
    // If the error_code is 909, wait for 10 seconds and then retry
    console.log('Export not ready, retrying in 10 seconds...')
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(checkExport(gc, exportId))
      }, 10000) // retry after 10 seconds
    })
  }
  return result
}

// Helper function that maps GetCourse export format to simple JSON object
function mapExportedData(
  response: ExportApiResponse<ExportedData>
): Record<string, string>[] {
  const { fields, items } = response.info
  return items.map((item: any) => {
    const mappedItem: Record<string, string> = {}
    item.forEach((value: any, index: any) => {
      mappedItem[fields[index]] = value
    })
    return mappedItem
  })
}

try {
  const gc = new GetCourse('belyaevdev.getcourse.ru', 'longapitoken')
  const exportResponse = await gc.account.exportUsers({
    created_at: {
      from: '2022-06-16',
      to: '2023-06-16',
    },
  })

  const exportResult = await checkExport(gc, exportResponse.info.export_id)

  const mappedExport = mapExportedData(exportResult)

  console.log(mappedExport)
}
catch (err) {
  if (err instanceof FormatError || err instanceof ServerError)
    console.error(err.response)
  else console.error(err)
}

Configuration

TS Config Base

tsconfig.json is used as the base config for the Typescript compiler.

Contribution

We welcome contributions! If you'd like to contribute, feel free to fork this repository, make your changes, and then submit a pull request.

Support

If you encounter any problems or have any questions, you can create an issue on the GitHub issues page.

Sponsors

DotSales

License

This project is licensed under the MIT License.

0.1.5

8 months ago

0.1.0

9 months ago

0.1.2

9 months ago

0.1.1

9 months ago

0.1.4

9 months ago

0.1.3

9 months ago

0.0.9

9 months ago

0.0.8

9 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.4

11 months ago

0.0.5

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago