diff --git a/Nest API Template.postman_collection.json b/Nest API Template.postman_collection.json index d945f30..16fc2c6 100644 --- a/Nest API Template.postman_collection.json +++ b/Nest API Template.postman_collection.json @@ -573,54 +573,82 @@ "script": { "type": "text/javascript", "exec": [ - "const path = pm.request.url.getPath();", - "const urlApi = pm.collectionVariables.get('url_api');", - "const token = pm.collectionVariables.get('token');", + "const URL_API = pm.collectionVariables.get(\"url_api\");", + "const AUTH_PATHS = {", + "\tme: \"/auth/me\",", + "\tsignIn: \"/auth/sign-in\",", + "\tsignUp: \"/auth/sign-up\",", + "};", "", - "if(path !== '/auth/me' && path !== '/auth/sign-in' && path !== '/auth/sign-up') {", - " pm.sendRequest({", - " url: `${urlApi}/auth/me`,", - " method: 'GET',", - " header: {", - " 'Content-Type': 'application/json',", - " 'X-Requested-With': 'XMLHttpRequest',", - " 'Authorization': `Bearer ${token}`", - " },", - " }, (err, res) => {", - " console.log(res);", - " if(res.code === 401) {", - " const email = pm.collectionVariables.get('email');", - " const password = pm.collectionVariables.get('password');", + "const signInRequest = () => {", + "\tconst email = pm.collectionVariables.get(\"email\");", + "\tconst password = pm.collectionVariables.get(\"password\");", "", - " pm.sendRequest({", - " url: `${urlApi}/auth/sign-in`,", - " method: 'POST',", - " header: {", - " 'Content-Type': 'application/json',", - " },", - " body: {", - " mode: 'raw',", - " raw: JSON.stringify({", - " \"email\": email,", - " \"password\": password,", - " }),", - " },", - " }, function (err, res) {", - " if(res.code === 404) {", - " console.log('Usuario no existe');", - " } else if(res.code === 401) {", - " console.log('Usuario sin autorizacion');", - " } else {", - " console.log(res);", - " const token = res.json().data.accessToken;", + "\tpm.sendRequest(", + "\t\t{", + "\t\t\turl: `${URL_API}${AUTH_PATHS.signIn}`,", + "\t\t\tmethod: \"POST\",", + "\t\t\theader: {", + "\t\t\t\t\"Content-Type\": \"application/json\",", + "\t\t\t\t\"X-Requested-With\": \"XMLHttpRequest\",", + "\t\t\t},", + "\t\t\tbody: {", + "\t\t\t\tmode: \"raw\",", + "\t\t\t\traw: JSON.stringify({", + "\t\t\t\t\temail: email,", + "\t\t\t\t\tpassword: password,", + "\t\t\t\t}),", + "\t\t\t},", + "\t\t},", + "\t\t(err, res) => {", + "\t\t\tconsole.log({ nameRequest: 'meRequest', res, err });", "", - " pm.collectionVariables.set('token', token);", - " }", + "\t\t\tif (res.code === 404) {", + "\t\t\t\tconsole.log(\"Usuario no existe\");", + "\t\t\t} else if (res.code === 401) {", + "\t\t\t\tconsole.log(\"Usuario sin autorizacion\");", + "\t\t\t} else {", + "\t\t\t\tconst token = res.json().data.accessToken;", "", - " });", - " }", - " });", - "}" + "\t\t\t\tpm.collectionVariables.set(\"token\", token);", + "\t\t\t}", + "\t\t}", + "\t);", + "};", + "", + "const meRequest = () => {", + "\tconst token = pm.collectionVariables.get(\"token\");", + "", + "\tpm.sendRequest(", + "\t\t{", + "\t\t\turl: `${URL_API}${AUTH_PATHS.me}`,", + "\t\t\tmethod: \"GET\",", + "\t\t\theader: {", + "\t\t\t\t\"Content-Type\": \"application/json\",", + "\t\t\t\t\"X-Requested-With\": \"XMLHttpRequest\",", + "\t\t\t\tAuthorization: `Bearer ${token}`,", + "\t\t\t},", + "\t\t},", + "\t\t(err, res) => {", + "\t\t\tconsole.log({ nameRequest: 'meRequest', res, err });", + "", + "\t\t\tif (res.code === 401) {", + "\t\t\t\tsignInRequest();", + "\t\t\t}", + "\t\t}", + "\t);", + "};", + "", + "const main = () => {", + "\tconst currentPath = pm.request.url.getPath();", + "\tconst isAuthPath = Object.values(AUTH_PATHS).includes(currentPath);", + "", + "\tif (!isAuthPath) {", + "\t\tmeRequest();", + "\t}", + "};", + "", + "main();" ] } },