1- import { Handler , APIGatewayProxyEventV2 , APIGatewayProxyResultV2 } from 'aws-lambda' ;
1+ import {
2+ Handler ,
3+ APIGatewayProxyEventV2 ,
4+ APIGatewayProxyResultV2 ,
5+ } from 'aws-lambda' ;
26import fetch from 'cross-fetch' ;
3- import { left , right , isRight } from 'fp-ts/Either'
4-
7+ import { left , right , isRight } from 'fp-ts/Either' ;
58
69const eventToRequest = ( source : string ) => {
710 const bodyString = Buffer . from ( source , 'base64' ) . toString ( 'ascii' ) ;
811 const body = new URLSearchParams ( bodyString ) ;
9- const paramNames = [ 'grant_type' , 'redirect_uri' , 'client_id' , 'client_secret' , 'code' ] ;
12+ const paramNames = [
13+ 'grant_type' ,
14+ 'redirect_uri' ,
15+ 'client_id' ,
16+ 'client_secret' ,
17+ 'code' ,
18+ ] ;
1019
11- const invalidParams = paramNames . filter ( name => ! body . has ( name ) ) ;
20+ const invalidParams = paramNames . filter ( ( name ) => ! body . has ( name ) ) ;
1221 if ( invalidParams . length > 0 ) {
1322 return right ( ( ) => `token request body ${ invalidParams } ` ) ;
1423 }
@@ -21,35 +30,37 @@ const eventToRequest = (source: string) => {
2130 code : body . get ( 'code' ) ! ,
2231 state : body . get ( 'state' ) ?? undefined ,
2332 } ) ) ;
24- }
33+ } ;
2534
26- export const handler : Handler < APIGatewayProxyEventV2 , APIGatewayProxyResultV2 > = async ( event , _context , _callback ) => {
35+ export const handler : Handler <
36+ APIGatewayProxyEventV2 ,
37+ APIGatewayProxyResultV2 | void
38+ > = async ( event , _context , callback ) => {
2739 if ( ! event . body ) {
28- return {
29- cookies : [ ] ,
40+ callback ( null , {
3041 statusCode : 400 ,
31- } ;
42+ } ) ;
43+ return ;
3244 }
3345 const result = eventToRequest ( event . body ) ;
3446 if ( isRight ( result ) ) {
35- return {
36- cookies : [ ] ,
47+ callback ( null , {
3748 statusCode : 400 ,
3849 body : result . right ( ) ,
39- } ;
50+ } ) ;
51+ return ;
4052 }
4153 const body = JSON . stringify ( result . left ( ) ) ;
4254 const response = await fetch ( 'https://github.com/login/oauth/access_token' , {
4355 method : 'POST' ,
4456 headers : {
4557 'Content-Type' : 'application/x-www-form-urlencoded; charset=utf-8' ,
46- ' Accept' : 'application/json'
58+ Accept : 'application/json' ,
4759 } ,
48- body
60+ body,
4961 } ) ;
50- return {
51- cookies : [ ] ,
62+ callback ( null , {
5263 statusCode : 200 ,
5364 body : JSON . stringify ( await response . json ( ) ) ,
54- } ;
55- }
65+ } ) ;
66+ } ;
0 commit comments