Giter VIP home page Giter VIP logo

mymicds-sdk's Introduction

MyMICDS-SDK

The official TypeScript client for connecting to the MyMICDS API

Installation

NPM

$ npm install @mymicds/sdk

CDN

For usage directly in the browser, add the following script tags to your HTML:

<script src="https://unpkg.com/@reactivex/[email protected]/dist/global/rxjs.umd.js"></script>
<script src="https://unpkg.com/@mymicds/[email protected]/dist/umd.js"></script>

Make sure to use the latest version of @mymicds/sdk@<version> from unpkg. See example.html for example usage.

Usage

import { MyMICDS, MyMICDSOptions } from '@mymicds/sdk';
import { mergeMap } from 'rxjs/operators';

const jwtStore = {
	jwt: null,
	remember: null
};

// You can pass a `MyMICDSOptions` object for configuring API client behavior
const options: MyMICDSOptions = {
	// URL for accessing MyMICDS API back-end
	baseURL: 'https://api.mymicds.net/v3',
	// How API client should retrieve any stored JWT
	// Return JWT string if available, otherwise return falsey value
	jwtGetter() {
		return jwtStore.jwt;
	},
	// How API client should save a JWT upon login
	// If `remember` is true, the JWT has a longer expiration and is intended to be saved in
	// long-term persistent storage (compared to being cleared after the current session is over).
	jwtSetter(jwt, remember) {
		jwtStore.jwt = jwt;
		jwtStore.remember = remember;
	},
	// How API client should delete a JWT
	// This is called upon logging out.
	jwtClear() {
		jwtStore.jwt = null;
		jwtStore.remember = null;
	},
	// Whether or not to activate observables for their respective modules (makes another API request on auth change)
	updateBackground: true, // Exposes MyMICDS.background.$ observable
	updateUserInfo: true // Exposes MyMICDS.user.$ observable
};

// These are the default configuration options if some/all of properties are omitted.
// Note that `localStorage` and `sessionStorage` may only be available in certain environments.
const defaultOptions: MyMICDSOptions = {
	baseURL: 'https://api.mymicds.net/v3',
	jwtGetter() {
		return sessionStorage.getItem('jwt') || localStorage.getItem('jwt');
	},
	jwtSetter(jwt: string, remember: boolean) {
		if (remember) {
			localStorage.setItem('jwt', jwt);
		} else {
			sessionStorage.setItem('jwt', jwt);
		}
	},
	jwtClear() {
		localStorage.removeItem('jwt');
		sessionStorage.removeItem('jwt');
	}
};

// How to instantiate an API client with options
const mymicds = new MyMICDS(options);

// All API methods return RxJS Observables.
// Note that storing the JWT is automatically taken care of by the API client via the JWT getter and setter options.
mymicds.auth
	.login({ user: 'llyle', password: 'Hunter2' })
	.pipe(
		// Use `switchMap` to chain Observables!
		switchMap(() => mymicds.schedule.get())
	)
	.subscribe(
		data => {
			console.log('data', data);
		},
		err => {
			console.log('err', err);
		}
	);

mymicds-sdk's People

Contributors

dependabot[bot] avatar michaelgira23 avatar nickbclifford avatar sbaumohl avatar

Watchers

 avatar  avatar

mymicds-sdk's Issues

Compile to UMD for direct usage on web

We want the option to directly use the SDK on web without needing a build tool like Webpack. We should have another file (umd.js) that compiles the SDK to UMD and bundle as many dependencies as we can. That way, we can use something like UNPKG for convenient usage directly on web.

I already have a branch with a prototype on 13b7d0a, but RxJS pipe operators not fully working.

Use ESLint

We should be able to just use @mymicds/eslint-config without any fuss.

Investigate published contents

When publishing an update to npm, I just noticed that the tarball includes all the sourcemap files. For bundle size reasons, we should think about whether these are worth keeping inside our published package.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.