From dd6d3c8531851efba51b34c3a5c099cd1f04cfc2 Mon Sep 17 00:00:00 2001
From: Andrew Israel <andrew@propelauth.com>
Date: Mon, 29 Jan 2024 13:54:35 -0800
Subject: [PATCH] Add fetch for query signup parameters

---
 package.json    | 114 ++++++++++++++++++++++++------------------------
 src/api/user.ts |  31 +++++++++++++
 src/auth.ts     |   7 +++
 src/index.ts    |   1 +
 4 files changed, 96 insertions(+), 57 deletions(-)

diff --git a/package.json b/package.json
index 2b645f5..d898090 100644
--- a/package.json
+++ b/package.json
@@ -1,62 +1,62 @@
 {
-  "name": "@propelauth/node",
-  "repository": {
-    "type": "git",
-    "url": "https://github.com/PropelAuth/node"
-  },
-  "version": "2.1.8",
-  "license": "MIT",
-  "keywords": [
-    "auth",
-    "node",
-    "user"
-  ],
-  "dependencies": {
-    "jsonwebtoken": "^9.0.0"
-  },
-  "devDependencies": {
-    "@rollup/plugin-commonjs": "^19.0.0",
-    "@rollup/plugin-node-resolve": "^13.0.0",
-    "@rollup/plugin-typescript": "^8.2.1",
-    "@types/express": "^4.17.11",
-    "@types/jest": "^27.0.1",
-    "@types/jsonwebtoken": "^8.5.1",
-    "@types/uuid": "^8.3.1",
-    "jest": "^27.0.6",
-    "nock": "^13.1.2",
-    "prettier": "^2.3.2",
-    "prettier-plugin-organize-imports": "^2.3.3",
-    "rimraf": "^3.0.2",
-    "rollup": "^2.52.7",
-    "rollup-plugin-peer-deps-external": "^2.2.4",
-    "ts-jest": "^27.0.5",
-    "typescript": "^4.2.4",
-    "uuid": "^8.3.2"
-  },
-  "scripts": {
-    "type-check": "tsc --noEmit",
-    "type-check:watch": "npm run type-check -- --watch",
-    "build:types": "tsc --emitDeclarationOnly",
-    "build:js": "rollup -c",
-    "build": "npm run test && npm run build:types && npm run build:js",
-    "test": "jest --silent",
-    "prepublishOnly": "npm run build"
-  },
-  "main": "dist/index.js",
-  "module": "dist/index.es.js",
-  "files": [
-    "dist"
-  ],
-  "jest": {
-    "testMatch": [
-      "**/test/*.ts"
+    "name": "@propelauth/node",
+    "repository": {
+        "type": "git",
+        "url": "https://github.com/PropelAuth/node"
+    },
+    "version": "2.1.9",
+    "license": "MIT",
+    "keywords": [
+        "auth",
+        "node",
+        "user"
     ],
-    "moduleFileExtensions": [
-      "ts",
-      "js"
+    "dependencies": {
+        "jsonwebtoken": "^9.0.0"
+    },
+    "devDependencies": {
+        "@rollup/plugin-commonjs": "^19.0.0",
+        "@rollup/plugin-node-resolve": "^13.0.0",
+        "@rollup/plugin-typescript": "^8.2.1",
+        "@types/express": "^4.17.11",
+        "@types/jest": "^27.0.1",
+        "@types/jsonwebtoken": "^8.5.1",
+        "@types/uuid": "^8.3.1",
+        "jest": "^27.0.6",
+        "nock": "^13.1.2",
+        "prettier": "^2.3.2",
+        "prettier-plugin-organize-imports": "^2.3.3",
+        "rimraf": "^3.0.2",
+        "rollup": "^2.52.7",
+        "rollup-plugin-peer-deps-external": "^2.2.4",
+        "ts-jest": "^27.0.5",
+        "typescript": "^4.2.4",
+        "uuid": "^8.3.2"
+    },
+    "scripts": {
+        "type-check": "tsc --noEmit",
+        "type-check:watch": "npm run type-check -- --watch",
+        "build:types": "tsc --emitDeclarationOnly",
+        "build:js": "rollup -c",
+        "build": "npm run test && npm run build:types && npm run build:js",
+        "test": "jest --silent",
+        "prepublishOnly": "npm run build"
+    },
+    "main": "dist/index.js",
+    "module": "dist/index.es.js",
+    "files": [
+        "dist"
     ],
-    "transform": {
-      "^.+\\.(ts|tsx)?$": "ts-jest"
+    "jest": {
+        "testMatch": [
+            "**/test/*.ts"
+        ],
+        "moduleFileExtensions": [
+            "ts",
+            "js"
+        ],
+        "transform": {
+            "^.+\\.(ts|tsx)?$": "ts-jest"
+        }
     }
-  }
 }
diff --git a/src/api/user.ts b/src/api/user.ts
index 4eeaa17..4db0364 100644
--- a/src/api/user.ts
+++ b/src/api/user.ts
@@ -511,3 +511,34 @@ export function deleteUser(authUrl: URL, integrationApiKey: string, userId: stri
         return true
     })
 }
+
+export type UserSignupQueryParams = {
+    userSignupQueryParameters: { [key: string]: string }
+}
+
+export async function fetchUserSignupQueryParams(
+    authUrl: URL,
+    integrationApiKey: string,
+    userId: string
+): Promise<UserSignupQueryParams | null> {
+    if (!isValidId(userId)) {
+        return Promise.resolve(null)
+    }
+
+    const httpResponse = await httpRequest(
+        authUrl,
+        integrationApiKey,
+        `${ENDPOINT_PATH}/${userId}/signup_query_parameters`,
+        "GET"
+    )
+    if (httpResponse.statusCode === 401) {
+        throw new Error("integrationApiKey is incorrect")
+    } else if (httpResponse.statusCode === 404) {
+        return null
+    } else if (httpResponse.statusCode && httpResponse.statusCode >= 400) {
+        throw new Error("Unknown error when fetching user signup query params")
+    }
+
+    const snakeCase = JSON.parse(httpResponse.response)
+    return { userSignupQueryParameters: snakeCase["user_signup_query_parameters"] }
+}
diff --git a/src/auth.ts b/src/auth.ts
index c883652..3efc095 100644
--- a/src/auth.ts
+++ b/src/auth.ts
@@ -48,6 +48,7 @@ import {
     fetchUserMetadataByQuery,
     fetchUserMetadataByUserIdWithIdCheck,
     fetchUsersByQuery,
+    fetchUserSignupQueryParams,
     fetchUsersInOrg,
     inviteUserToOrg,
     InviteUserToOrgRequest,
@@ -57,6 +58,7 @@ import {
     UpdateUserMetadataRequest,
     updateUserPassword,
     UpdateUserPasswordRequest,
+    UserSignupQueryParams,
     UsersInOrgQuery,
     UsersPagedResponse,
     UsersQuery,
@@ -142,6 +144,10 @@ export function initBaseAuth(opts: BaseAuthOptions) {
         })
     }
 
+    function fetchUserSignupQueryParamsWrapper(userId: string): Promise<UserSignupQueryParams | null> {
+        return fetchUserSignupQueryParams(authUrl, integrationApiKey, userId)
+    }
+
     function fetchBatchUserMetadataByUserIds(
         userIds: string[],
         includeOrgs?: boolean
@@ -349,6 +355,7 @@ export function initBaseAuth(opts: BaseAuthOptions) {
         fetchUserMetadataByUserId,
         fetchUserMetadataByEmail,
         fetchUserMetadataByUsername,
+        fetchUserSignupQueryParams: fetchUserSignupQueryParamsWrapper,
         fetchBatchUserMetadataByUserIds,
         fetchBatchUserMetadataByEmails,
         fetchBatchUserMetadataByUsernames,
diff --git a/src/index.ts b/src/index.ts
index dbe2169..3f6e384 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -7,6 +7,7 @@ export {
     InviteUserToOrgRequest,
     UpdateUserEmailRequest,
     UpdateUserMetadataRequest,
+    UserSignupQueryParams,
     UsersInOrgQuery,
     UsersPagedResponse,
     UsersQuery,
