키워드
싱글톤, closure, 가비지 콜렉터, 모듈
모든 프로젝트에서 사용할 함수를 모아서 closure로 만든다.
closure는 ~~이런 장점이 있고 단점이 있다.
그렇기 때문에 자주 사용하는 함수를 모아서 싱글톤으로 구현한다. 그래서 다양한 기능을 하는 util모듈과 서버와 통신하는 request 모듈을 구현하였다.
scripts/request.js
import Amplify, { Auth, API, graphqlOperation } from 'aws-amplify';
import awsconfig from './aws-exports';
import * as mutations from '../src/graphql/mutations.js';
import * as queries from '../src/graphql/queries.js';
Amplify.configure(awsconfig);
const cognitoFlag = true;
const myExceptions = Object.freeze({
UserNotConfirmedException: 1,
});
const MyRequest = (function () {
const host = 'http://localhost';
const port = 3000;
/* Use this function for testing when the rest api is not yet implemented */
function requestToServerTest(input, apiName) {
return new Promise((resolve, reject) => {
let res = {
success: Math.floor(Math.random() * 2) == 0 ? true : false,
code: Math.floor(Math.random() * 100),
msg: 'hello',
};
if (res.success === true) {
resolve(res);
} else {
reject(res);
}
});
}
function requestToServer(input, apiName) {
const api = `${this.host}:${this.port}/${apiName}`;
const header = new Headers();
header.append('Content-Type', 'application/json');
const request = new Request(api, {
header: header,
method: 'POST',
body: input,
});
return new Promise((reslove, reject) => {
return fetch(request);
})
.then((response) => {
return response.json();
})
.then((jsonRes) => {
if (jsonRes.success == true) {
reslove(jsonRes);
} else {
reject(jsonRes);
}
})
.catch((error) => {
console.error(error);
const errRes = { success: false, code: null, message: 'unknown error' };
reject(errRes);
});
}
function signUp(email, password) {
let ret = null;
/* create input of json format */
const input = { email: email, password: password };
const jsonInput = JSON.stringify(input);
/* request to server */
if (cognitoFlag === true) {
ret = Auth.signUp({
username: email,
password: password,
attributes: {
nickname: '',
gender: '',
'custom:additional_verified': 0,
},
});
} else {
ret = requestToServer(jsonInput, '/v1/signup');
}
return ret;
}
function confirmSignUp(email, code) {
let ret = null;
/* create input of json format */
const input = { email: email, code: code };
const jsonInput = JSON.stringify(input);
/* request to server */
if (cognitoFlag === true) {
ret = Auth.confirmSignUp(email, code);
} else {
ret = requestToServer(jsonInput, '/v1/confirmSignUp');
}
return ret;
}
function resendCode(email) {
let ret = null;
/* create input of json format */
const input = { email: email };
const jsonInput = JSON.stringify(input);
if (cognitoFlag === true) {
ret = Auth.resendSignUp(email);
} else {
ret = requestToServer(jsonInput, '/v1/resendCode');
}
return ret;
}
function checkNickname(nickname) {
let ret = null;
const input = { nickname: nickname };
const jsonInput = JSON.stringify(input);
if (cognitoFlag === true) {
ret = API.graphql(
graphqlOperation(queries.byNickname, { nickname: nickname })
);
} else {
ret = requestToServerTest(jsonInput, '/v1/checkNickname');
}
return ret;
}
return {
signUp: signUp,
confirmSignUp: confirmSignUp,
resendCode: resendCode,
checkNickname: checkNickname,
};
})();