forked from kolplattformen/skolplattformen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 Possibly first working version
- Loading branch information
1 parent
084e961
commit 0e4acba
Showing
16 changed files
with
1,217 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,5 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
run.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,46 @@ | ||
# 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). | ||
|
||
## How to use | ||
|
||
### Installing | ||
|
||
`npm i -S @skolplattformen/embedded-api` or `yarn add @skolplattformen/embedded-api` | ||
|
||
### Calling | ||
|
||
#### Import and init | ||
|
||
```javascript | ||
import init from "@skolplattformen/embedded-api"; | ||
|
||
const api = init(fetch); | ||
``` | ||
|
||
#### Login | ||
|
||
```javascript | ||
api.on("login", () => { | ||
// keep going | ||
}); | ||
|
||
const loginStatus = await api.login("YYYYMMDDXXXX"); | ||
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 | ||
|
||
```javascript | ||
// List children | ||
const children = await api.getChildren(); | ||
|
||
// Get calendar | ||
const calendar = await api.getCalendar(children[0].id); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
import { AxiosAdapter, AxiosInstance } from 'axios' | ||
import routes from './routes' | ||
import { Child } from './types' | ||
import { CalendarItem, Child, Fetch, RequestInit } from './types' | ||
import { etjanst, child, calendarItem } from './parse' | ||
|
||
export const list = (client: AxiosInstance) => async (): Promise<Child[]> => { | ||
export const list = (fetch: Fetch, init?: RequestInit) => async (): Promise<Child[]> => { | ||
const url = routes.children | ||
const response = await client.get(url) | ||
return response.data | ||
const response = await fetch(url, init) | ||
const data = await response.json() | ||
return etjanst(data).map(child) | ||
} | ||
|
||
export const details = (client: AxiosAdapter) => async (id: string): Promise<Child> => ({}) | ||
export const calendar = (fetch: Fetch, init?:RequestInit) => async (childId: string): Promise<CalendarItem[]> => { | ||
const url = routes.calendar(childId) | ||
const response = await fetch(url, init) | ||
const data = await response.json() | ||
console.log(etjanst(data)) | ||
return etjanst(data).map(calendarItem) | ||
} | ||
|
||
// export const details = (_fetch: Fetch) => async (_id: string): Promise<Child> => ({}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,43 @@ | ||
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios' | ||
import { list } from './children' | ||
import { checkStatus, getCookies, login } from './login' | ||
import { EventEmitter } from 'events' | ||
import { | ||
checkStatus, getSessionCookie, login, LoginStatus, | ||
} from './login' | ||
import { CalendarItem, Child, Fetch, RequestInit } from './types' | ||
import { calendar, list } from './children' | ||
|
||
const pause = async (ms: number) => new Promise<void>((r) => setTimeout(r, ms)) | ||
class Api extends EventEmitter { | ||
private fetch: Fetch | ||
|
||
const emitter = new EventEmitter() | ||
const init = () => { | ||
const config: AxiosRequestConfig = { | ||
headers: { | ||
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.87 Safari/537.36' | ||
}, | ||
maxRedirects: 0, | ||
withCredentials: true | ||
private session?: RequestInit | ||
|
||
constructor(fetch: Fetch) { | ||
super() | ||
this.fetch = fetch | ||
} | ||
|
||
async login(personalNumber: string): Promise<LoginStatus> { | ||
const ticket = await login(this.fetch)(personalNumber) | ||
const loginStatus = checkStatus(this.fetch)(ticket) | ||
loginStatus.on('OK', async () => { | ||
const sessionCookie = await getSessionCookie(this.fetch)() | ||
this.session = { headers: { Cookie: sessionCookie } } | ||
|
||
this.emit('login') | ||
}) | ||
return loginStatus | ||
} | ||
const client = axios.create(config) | ||
let cookies: any = {} | ||
|
||
client.interceptors.request.use((config) => { | ||
console.log('request', config.method, config.url) | ||
config.headers.Cookie = Object.entries(cookies) | ||
.map(([key, value]) => `${key}=${value}`) | ||
.join('; ') | ||
return config | ||
}) | ||
client.interceptors.response.use((response) => { | ||
console.log('response', response.status, response.statusText, response.headers['set-cookie']) | ||
if (response.headers['set-cookie']) { | ||
const setCookies: string[] = response.headers['set-cookie'] | ||
setCookies.map((c) => c.split('=')).forEach(([key, value]) => cookies[key] = value) | ||
} | ||
return response | ||
}) | ||
|
||
return { | ||
...emitter, | ||
login: async (personalNumber: string) => { | ||
const ticket = await login(client)(personalNumber) | ||
await pause(1000) | ||
const check = checkStatus(client)(ticket) | ||
check.on('OK', async () => { | ||
console.log('get cookie') | ||
const newCookies = await getCookies(client)() | ||
cookies = {...cookies, ...newCookies} | ||
console.log(cookies) | ||
|
||
emitter.emit('login') | ||
}) | ||
return check | ||
}, | ||
getChildren: async () => { | ||
const result = await list(client)() | ||
return result | ||
}, | ||
|
||
async getChildren(): Promise<Child[]> { | ||
const data = await list(this.fetch, this.session)() | ||
return data | ||
} | ||
|
||
async getCalendar(childId: string): Promise<CalendarItem[]> { | ||
const data = await calendar(this.fetch, this.session)(childId) | ||
return data | ||
} | ||
} | ||
|
||
export default init | ||
export default function init(fetch: Fetch) { | ||
return new Api(fetch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.