Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions js/axios/api-keys/fetchKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ const projectId = process.env.PROJECT_ID;

async function fetchApiKeys(filters) {
try {
const {data} = await axios
.get(`${apiUrl}/api-keys${filters ? `?${filters}` : ''}`, {
const { data } = await axios.get(
`${apiUrl}/api-keys${filters ? `?${filters}` : ""}`,
{
headers: {
ContentType: "application/json",
"x-api-key": accessKey,
},
});
}
);
return data;
} catch (error) {
if (error.response) {
Expand All @@ -27,8 +29,9 @@ async function fetchApiKeys(filters) {
}

function getFiltersQuery(filters) {
return Object.entries(filters).map(([key, value]) => `${key}=${value}`)
.join('&');
return Object.entries(filters)
.map(([key, value]) => `${key}=${value}`)
.join("&");
}

const filters = "";
Expand All @@ -40,23 +43,29 @@ const filters = "";
(async () => {
// fetch all keys
const apiKeys = await fetchApiKeys(filters);
console.log('All Keys', apiKeys);
console.log("All Keys", apiKeys);

// get keys with a specific name
const nameFilteredKeys = await fetchApiKeys(getFiltersQuery({
name: "new api key",
}));
console.log('Keys filtered using name', nameFilteredKeys);
// fetch keys with a specific name (1st one from the list returned previously)
const nameFilteredKeys = await fetchApiKeys(
getFiltersQuery({
name: apiKeys.length === 0 ? "YOUR_NAME_FILTER" : apiKeys[0].name,
})
);
console.log("Keys filtered using name", nameFilteredKeys);

// fetch keys where customUserId is null
const customUserIdFilteredKey = await fetchApiKeys(getFiltersQuery({
customUserId: null,
}));
console.log('Keys filtered using customUserId', customUserIdFilteredKey);
const customUserIdFilteredKey = await fetchApiKeys(
getFiltersQuery({
customUserId: null,
})
);
console.log("Keys filtered using customUserId", customUserIdFilteredKey);

// fetch inactive (revoked) keys
const inactiveKeys = await fetchApiKeys(getFiltersQuery({
isActive: false,
}));
console.log('Inactive (revoked) keys', inactiveKeys);
const inactiveKeys = await fetchApiKeys(
getFiltersQuery({
isActive: false,
})
);
console.log("Inactive (revoked) keys", inactiveKeys);
})();
2 changes: 1 addition & 1 deletion js/npm/api-keys/createKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function createApiKeySample() {
featureFlagOne: true,
featureFlagTwo: false,
},
expiry: new Date("2023-09-01 00:00:00"), // optional expiry date
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this works fine, expiry is expected to be a Date object, we want the users to use the library just like any other JS code, passing expiry as a date feels more natural in a JS environment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the comment before. Can you test using the JS Date object?

expiry: "2023-09-01 00:00:00", // optional expiry date
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one works, but I'm curios on why you're favoring expiry as string instead of a date object 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The date object was causing an error when posting to the server. I removed it for speed!

rateLimitConfigs: {
rateLimit: 60,
rateLimitTtl: 60,
Expand Down
24 changes: 11 additions & 13 deletions js/npm/api-keys/fetchKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,34 @@ require("dotenv").config();
const TheAuthAPI = require("theauthapi").default;

const apiUrl = process.env.production
? "https://api.theauthapi.com"
: process.env.TESTING_URL;
? "https://api.theauthapi.com"
: process.env.TESTING_URL;
const theAuthAPI = new TheAuthAPI(process.env.ACCESS_TOKEN, { host: apiUrl });


async function fetchKeysSample() {
// fetch all api keys
const apiKeys = await theAuthAPI.apiKeys.getKeys();
console.log('all keys', apiKeys);
console.log("all keys", apiKeys);

// fetch keys with a specific name
// fetch keys with a specific name (1st one from the list returned previously)
const nameFilteredKeys = await theAuthAPI.apiKeys.getKeys({
name: "new api key"
name: apiKeys.length === 0 ? "YOUR_NAME_FILTER" : apiKeys[0].name,
});
console.log('Keys filtered using name', nameFilteredKeys);
console.log("Keys filtered using name", nameFilteredKeys);

// fetch keys where customUserId is null
const customUserIdFilteredKey = await theAuthAPI.apiKeys.getKeys({
customUserId: null,
})
console.log('Keys filtered using customUserId', customUserIdFilteredKey);
customUserId: "null",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think leaving this as null instead of "null" is better, it feels more natural to pass null as null instead of a string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server borked with the null value.

});
console.log("Keys filtered using customUserId", customUserIdFilteredKey);

// fetch inactive (revoked) keys
const inactiveKeys = await theAuthAPI.apiKeys.getKeys({
isActive: false,
})
console.log('Inactive (revoked) keys', inactiveKeys);
});
console.log("Inactive (revoked) keys", inactiveKeys);
}

(async () => {
await fetchKeysSample();
})();