yalla-auth-typescript-client
v0.5.0
Published
TypeScript client for
Maintainers
Readme
@
This generator creates TypeScript/JavaScript client that utilizes fetch-api.
Building
To build and compile the typescript sources to javascript use:
npm install
npm run buildPublishing
First build the package then run .publish
Add CHANGELOG and README updates before publish
Consuming
Navigate to the folder of your consuming project and run one of the following commands.
published:
npm install @ --saveunPublished (not recommended):
npm install PATH_TO_GENERATED_PACKAGE --saveUsage
Below code snippet shows exemplary usage of the configuration and the API based
on the typical Auth example used for OpenAPI.
import { AuthService } from "yalla-auth-typescript-client";
const auth_api = AuthService.getInstance(<AUTH_URL>);
// Covers all auth methods included in your OpenAPI yaml definition
const authConfig: your_api.AuthMethodsConfiguration = {
"api_key": "YOUR_API_KEY"
}
// Implements a simple middleware to modify requests before (`pre`) they are sent
// and after (`post`) they have been received
class Test implements your_api.Middleware {
pre(context: your_api.RequestContext): Promise<your_api.RequestContext> {
// Modify context here and return
return Promise.resolve(context);
}
post(context: your_api.ResponseContext): Promise<your_api.ResponseContext> {
return Promise.resolve(context);
}
}
// Create configuration parameter object
const configurationParameters = {
httpApi: new your_api.JQueryHttpLibrary(), // Can also be ignored - default is usually fine
baseServer: your_api.servers[0], // First server is default
authMethods: authConfig, // No auth is default
promiseMiddleware: [new Test()],
}
// Convert to actual configuration
const config = auth_api.createConfiguration(configurationParameters);
// Use configuration with your_api
const api = AuthService.getInstance(<AUTH_URL>);
your_api.Pet p = new your_api.authApi.Pet();
p.name = "My new pet";
p.photoUrls = [];
p.tags = [];
p.status = "available";
Promise<your_api.Pet> createdPet = api.addPet(p);
auth_api.validateToken
Exports
validateToken
auth_api.validateToken(token: string)
call auth/validate_token and update localStorage
getToken
get token from local storage
setToken
set token in local storage
authenticate
validate that the token in local storage is valid call redirectToLogin otherwise
redirectToLogin
redirect to [AUTH_PATH]/users/sign_in?redirect_uri=<current_path>
authMiddleware
checks if token param in request url set it on local storage authenticate call redirectToLogin if unautorized
Vue js example
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: "/",
name: "home",
component: OpenAPIHelperView,
beforeEnter: (to, from, next) => {
AuthService.getInstance(<AUTH_URL>).authMiddleware(); // Runs only for this route
next();
}
},
],
});import { AuthService } from "yalla-auth-typescript-client";
const auth_api = AuthService.getInstance(<AUTH_URL>);
onMounted(() => {
auth_api.authenticate();
});