# api42tunnell

Latest version **1.0.0** (published 2022-07-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install api42tunnell
pnpm add api42tunnell
yarn add api42tunnell
bun add api42tunnell
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-07-03 |
| First published | 2022-07-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 1 |
| Unpacked size | 73.8 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| Author | hamza chakoubi |
| Maintainers | hchakoub |

## Links

- npm: https://www.npmjs.com/package/api42tunnell
- npm.io page: https://npm.io/package/api42tunnell

## Dependencies (1)

- [axios](https://npm.io/package/axios.md) ^0.27.2

## Recent versions

- 1.0.0 (latest) — 2022-07-03

## README

# api42tunnel 


api42tunnel is an small package that support typescript to help you use 42Api 

## installation :

to install api42tunnel use :

	npm i api42tunnel

## how to use :


### example of how to use api42tunnel on nestJs project

	- Auth class

- app.controller.ts :
```typescript

import { Controller, Get, Req } from '@nestjs/common';
import { AppService } from './app.service';
import { Request } from 'express';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get("login")
  login(): string {
    const uid = ""; //  uid that you got where you register you application in intra
    const redirect_uri = "http%3A%2F%2Flocalhost%3A3000%2FgetToken"; // you redirection url incoded
    return `<a href='https://api.intra.42.fr/oauth/authorize?client_id=${uid}&redirect_uri=${redirect_uri}&response_type=code'>LOGIN WITH INTRA</a>` // this is url to login with intra
  }

  @Get("getToken")
  async getToken(@Req() request: Request)  {
    const response = await this.appService.getAccessToken(request.query.code);
    
    return response;
  }
}


```

first we have to set our controller, we have to create login method that we return a simple link from it that have our application id `uid` and our application redirect url `redirect_uri` , this link will redirect us to get autorization from the user, after we get autorized we will be redirected to `redirect_uri` with autorization code as parameter.

after that we catch that url with that url with `getToken()` method, and then we take the code from parameters and send it to `getAccessToken()` on appService, look at the example bellow.

- app.service.ts
```typescript
import { Injectable } from '@nestjs/common';
import { Auth } from 'api42tunnel';
import { TokenDto } from './DTOs/tokenDto';

@Injectable()
export class AppService {
  uid = "YOUR_USER_ID"; // uid that you get whene you create your application on the intra
  secret = "YOUR_SECRET"; // secret that you get whene you create your application on the intra
  redirect_url = "REDIRECTION_URL"; // the redirection url (you have to put it in redirection url whene you create your application, it is the url that you will be redirected to, whide authorization code when you authorize the application)

  async getAccessToken(code: string): Promise<string> {
    const tunnel  = new Auth({ uid: this.uid, secret : this.secret, redirect_url: this.redirect_url});
    const response: any = await tunnel.getToken(code);
    const token: TokenDto = response.data;
    return token.access_token;
  }
}

```

on out `app.service.ts` we have to import the class `{ Auth }` from `api42tunnel` , then we have to instentiate an object from this class that takes and object as parameter, object defined bellow :

```typescript
{
	uid: string,
	secret: string,
	redirect_url: string
}
```
the attributes of this object explained on the example above.


after that we have to call the method `getToken()` and give it the autorization code as parameter and it will return an access_token as a string.




	- Tunnel class

to use the Tunnel class you have to import `{ Tunnel }` from `api42tunnel`, the constructor of this class takes the access_token as parameter.

EXAMPLE :

```typescript

import { Tunnel } from 'api42tunnel';

const tunnel = new Tunnel("YOUR_ACCESS_TOKEN");

tunnel.getUserById("USER ID AS NUMBER").then(response => {
	console.log(response);
}).catch(error => {
	console.log(error);
})

```

---
_Source: https://npm.io/package/api42tunnell · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
