skolplattformen-backup/libs/api-skolplattformen
Kajetan Kazimierczak d51cfe36fe
chore: 🤖 Updated TimeTable URLs (#670)
* chore: 🤖 Updated TimeTable URLs

* test: 💍 fixed failing tests
2024-04-09 20:13:11 +02:00
..
.vscode Moved to libs directory 2021-10-03 11:32:39 +02:00
__mocks__/@react-native-cookies fix: 🐛 Readded mock, ts config to api 2021-11-09 11:46:45 +01:00
lib chore: 🤖 Updated TimeTable URLs (#670) 2024-04-09 20:13:11 +02:00
.babelrc Fix tests for `hooks` 2021-10-15 22:36:48 +02:00
.eslintrc style: 💄 Add eslint rules to api-skolplattformen and autofix 2022-04-24 20:27:10 +02:00
.gitignore fix: 🐛 added dist to ignore 2021-11-27 20:53:52 +01:00
.prettierrc fix: lint and prettier fixes 2021-10-06 22:47:50 +02:00
.releaserc Moved to libs directory 2021-10-03 11:32:39 +02:00
LICENSE Moved to libs directory 2021-10-03 11:32:39 +02:00
README.md feat: 🎸 Fix image load and typescript errors (#570) 2021-12-02 15:34:15 +01:00
config.json Moved to libs directory 2021-10-03 11:32:39 +02:00
jest.config.js fix: change jest config for api's 2021-12-16 11:52:21 +01:00
package.json refactor: 💡 Switched to skolplattformen-app instead 2021-12-01 11:13:50 +01:00
project.json feat: 🎸 bump to RN 0.66 + upgrade dependencies (#521) 2021-12-16 08:22:20 +01:00
run.js refactor: 💡 Switched to skolplattformen-app instead 2021-12-01 11:13:50 +01:00
test-setup.ts style: 💄 Add eslint rules to api-skolplattformen and autofix 2022-04-24 20:27:10 +02:00
test.md Moved to libs directory 2021-10-03 11:32:39 +02:00
tsconfig.json fix: 🐛 Fixes TS config + lint etc 2021-11-12 13:46:25 +01:00
tsconfig.lib.json fix: 🐛 Fixes TSconfig for all projects 2021-11-09 14:41:01 +01:00
tsconfig.spec.json fix: 🐛 Fixes TSconfig for all projects 2021-11-09 14:41:01 +01:00

README.md

embedded-api

Since the proxy was blocked (and also deemed a bad idea by some), this is a reboot of the API running in process in the app(s).

Installing

npm i -S @skolplattformen/embedded-api or yarn add @skolplattformen/embedded-api

Calling

Import and init

Since fetch and cookies behave distinctly different in node, react-native and the browser, the concrete implementation of fetch and cookie handler must be injected.

react-native

import init from '@skolplattformen/api'
import CookieManager from '@react-native-cookies/cookies'

const api = init(fetch, () => CookieManager.clearAll())

node

import init from '@skolplattformen/api'
import nodeFetch from 'node-fetch'
import fetchCookie from 'fetch-cookie/node-fetch'
import { CookieJar } from 'tough-cookie'

const cookieJar = new CookieJar()
const fetch = fetchCookie(nodeFetch, cookieJar)

const api = init(fetch, cookieJar)

Login / logout

api.on('login', async () => {
  // do stuff
  console.log(api.isLoggedIn) // true
  await api.logout()
})
api.on('logout', () => {
  // handle logout
  console.log(api.isLoggedIn) // false
}

const loginStatus = await api.login('YYYYMMDDXXXX')
window.open(
  `https://app.bankid.com/?autostarttoken=${loginStatus.token}&redirect=null`
)

loginStatus.on('PENDING', () => console.log('BankID app not yet opened'))
loginStatus.on('USER_SIGN', () => console.log('BankID app is open'))
loginStatus.on('ERROR', () => console.log('Something went wrong'))
loginStatus.on('OK', () =>
  console.log('BankID sign successful. Session will be established.')
)

Loading data

// Get current user
const user = await api.getUser()

// List children from Etjanster
const children = await api.getChildren()

// Get school calendar
const calendar = await api.getCalendar(children[0])

// Get classmates - disabled for reasons
// const classmates = await api.getClassmates(children[0])

// Get student's personal schedule
import { DateTime } from 'luxon'

const from = DateTime.local()
const to = DateTime.local().plus({ week: 1 })
const schedule = await api.getSchedule(children[0], from, to)

// Get news
const news = await api.getNews(children[0])

// Get news details
const newsDetails = await api.getNewsDetails(children[0], news[0])

// Get menu
const menu = await api.getMenu(children[0])

// Get notifications
const notifications = await api.getNotifications(children[0])

// Get list of children from Skola24 (because of course it's different *DERP*)
const skola24Children = await getSkola24Children()

// Get timetable
const weekNumber = 15
const year = 2021
const timetable = await api.getTimetable(skola24Children[0], weekNumber, year)

It is possible to resurrect a logged in session by manually setting the session cookie.

const sessionCookie = 'some value'

api.setSessionCookie(sessionCookie) // will trigger `on('login')` event and set `.isLoggedIn = true`

Fake user

Login with personal number 12121212121212, 201212121212 or 1212121212 and api will be put into fake mode. Static data will be returned and no calls to backend will be made.

The LoginStatusChecker returned by the login method will have .token set to 'fake'.

Try it out

  1. Clone and enter repo: git clone git@github.com:kolplattformen/embedded-api.git && cd embedded-api
  2. Install dependencies: yarn
  3. Build package: yarn build
  4. Run example: node run [your personal number]
  5. Sign in with mobile BankID