feat: 🎸 getUser (#19)

Returns currrenctly logged in user

 Closes: #9
This commit is contained in:
Johan Öbrink 2020-12-21 17:01:22 +01:00 committed by GitHub
parent 02bf08a12e
commit 39b62b7b37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 75 additions and 8 deletions

View File

@ -14,6 +14,7 @@ module.exports = {
// e.g. "@typescript-eslint/explicit-function-return-type": "off",
// '@typescript-eslint/indent': ['error', 2],
'@typescript-eslint/semi': [2, 'never'],
'max-len': ['error', { code: 120, 'ignoreUrls': true }]
'max-len': ['error', { code: 120, 'ignoreUrls': true }],
'import/prefer-default-export': 0,
},
}

View File

@ -65,6 +65,9 @@ loginStatus.on("OK", () =>
### Loading data
```javascript
// Get current user
const user = await api.getUser();
// List children
const children = await api.getChildren();

View File

@ -10,6 +10,7 @@ import {
calendar, classmates, list, menu, schedule,
} from './children'
import { news, News } from './news'
import { user } from './user'
interface AsyncishFunction { (): void | Promise<void> }
@ -49,6 +50,11 @@ export class Api extends EventEmitter {
return loginStatus
}
async getUser(): Promise<any> {
const data = await user(this.fetch, this.session)()
return data
}
async getChildren(): Promise<Child[]> {
const data = await list(this.fetch, this.session)()
return data

View File

@ -1,6 +1,6 @@
import * as moment from 'moment'
import {
etjanst, newsItem, EtjanstResponse, child, calendarItem, classmate, scheduleItem, menuItem,
etjanst, newsItem, EtjanstResponse, child, calendarItem, classmate, scheduleItem, menuItem, user,
} from "./parse"
import { NewsItem } from "./types"
@ -306,5 +306,28 @@ describe('parse', () => {
}])
})
})
describe('user', () => {
let userResponse: any
beforeEach(() => {
userResponse = {
socialSecurityNumber: '197106171635',
isAuthenticated: true,
userFirstName: 'Per-Ola',
userLastName: 'Assarsson',
userEmail: 'per-ola.assarsson@dodgit.com',
notificationId: 'B026594053D44299AB64ED81990B49C04D32F635C9A3454A84030439BFDDEF04'
}
})
it('parses user correctly', () => {
expect(user(userResponse)).toEqual({
personalNumber: '197106171635',
firstName: 'Per-Ola',
lastName: 'Assarsson',
email: 'per-ola.assarsson@dodgit.com',
isAuthenticated: true,
notificationId: 'B026594053D44299AB64ED81990B49C04D32F635C9A3454A84030439BFDDEF04',
})
})
})
})
})

View File

@ -2,7 +2,7 @@ import * as moment from 'moment'
import * as h2m from 'h2m'
import { htmlDecode } from 'js-htmlencode'
import {
CalendarItem, Child, Classmate, Guardian, MenuItem, NewsItem, ScheduleItem,
CalendarItem, Child, Classmate, Guardian, MenuItem, NewsItem, ScheduleItem, User,
} from './types'
const camel = require('camelcase-keys')
@ -20,6 +20,17 @@ export const etjanst = (response: EtjanstResponse): any | any[] => {
return camel(response.Data, { deep: true })
}
export const user = ({
socialSecurityNumber, isAuthenticated, userFirstName, userLastName, userEmail, notificationId,
}: any): User => ({
personalNumber: socialSecurityNumber,
firstName: userFirstName,
lastName: userLastName,
email: userEmail,
isAuthenticated,
notificationId,
})
export const child = ({
id, sdsId, name, status, schoolId,
}: any): Child => ({

View File

@ -142,3 +142,12 @@ export interface MenuItem {
title: string
description: string
}
export interface User {
personalNumber: string
isAuthenticated: boolean
firstName: string
lastName: string
email: string | null
notificationId: string
}

10
lib/user.ts Normal file
View File

@ -0,0 +1,10 @@
import routes from './routes'
import { user as _user } from './parse'
import { Fetch, RequestInit } from './types'
export const user = (fetch: Fetch, init?: RequestInit) => async (): Promise<any> => {
const url = routes.user
const response = await fetch(url, init)
const data = await response.json()
return _user(data)
}

14
run.js
View File

@ -28,8 +28,12 @@ async function run() {
api.on('login', async () => {
console.log('Logged in')
console.log('children')
const children = await api.getChildren()
console.log('user')
const user = await api.getUser()
console.log(user)
// console.log('children')
// const children = await api.getChildren()
// console.log(children)
// console.log('calendar')
@ -48,9 +52,9 @@ async function run() {
// const news = await api.getNews(children[0])
// console.log(news)
console.log('menu')
const menu = await api.getMenu(children[0])
console.log(menu)
// console.log('menu')
// const menu = await api.getMenu(children[0])
// console.log(menu)
await api.logout()
})