Skip to content

Commit 7335d1a

Browse files
authored
Merge pull request #23 from owenr88/release/1.2.8
1.2.8
2 parents 01436ed + 31feed0 commit 7335d1a

File tree

7 files changed

+40
-19
lines changed

7 files changed

+40
-19
lines changed

package-lock.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cognito-export/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "cognito-export",
33
"description": "A simple CLI tool and JavaScript package for exporting users from an AWS Cognito user pool.",
4-
"version": "1.2.7",
4+
"version": "1.2.8",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"bin": "dist/cli.js",

packages/cognito-export/src/CognitoBase.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CognitoIdentityProvider } from "@aws-sdk/client-cognito-identity-provider";
22
import chalk from "chalk";
3+
import { z } from "zod";
34

45
export type CognitoExportProps = {
56
userPoolId: string;
@@ -11,6 +12,7 @@ class CognitoBase {
1112
protected verbose: boolean;
1213
protected userPoolId: string;
1314
protected cognito: CognitoIdentityProvider | undefined;
15+
protected customAttributes: Record<string, z.ZodType<string | number>> = {}
1416

1517
constructor(options: CognitoExportProps) {
1618
this.verbose = !!options.verbose;
@@ -78,6 +80,16 @@ class CognitoBase {
7880
);
7981
}
8082

83+
// Set the custom attributes
84+
userPool.UserPool?.SchemaAttributes?.forEach((attr) => {
85+
if (attr.Name && attr.Name.startsWith("custom:")) {
86+
this.customAttributes[attr.Name] = attr.AttributeDataType === "String" ? z.string() : z.number();
87+
}
88+
});
89+
if(Object.keys(this.customAttributes).length) {
90+
this.log('Extracted custom attributes: ' + Object.keys(this.customAttributes).join(','))
91+
}
92+
8193
this.log("Found user pool");
8294
} catch (e: any) {
8395
this.log(e.message, "error");

packages/cognito-export/src/exporter.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { z } from "zod";
55
import {
66
ImportRecordSchema,
77
ImportRecordSchemaType,
8-
extractCustomAttributeKeys,
9-
parseUserAttributes,
8+
parseUserAttributes
109
} from "zod-cognito";
1110
import CognitoBase from "./CognitoBase";
1211

@@ -28,21 +27,17 @@ export class CognitoExport extends CognitoBase {
2827
// Get the users
2928
const res = await this.cognito?.listUsers(params);
3029

30+
// Add these to the pool custom attributes
31+
const customAttr = {
32+
...this.customAttributes,
33+
sub: z.string(),
34+
}
35+
const customAttributes = z.object(customAttr);
36+
3137
// Loop through the users and parse them into the array
3238
res?.Users?.forEach((user) => {
3339
if (users.length >= limit) return false;
3440

35-
// Convert the customAttributeKeys to an object
36-
const customAttributeKeys = extractCustomAttributeKeys(
37-
user.Attributes ?? []
38-
);
39-
const customAttr: Record<string, z.ZodType<string | number>> = {};
40-
customAttributeKeys?.forEach((key) => {
41-
customAttr[key] = z.union([z.string(), z.number()]);
42-
});
43-
customAttr.sub = z.string(); // Add this to exported data
44-
const customAttributes = z.object(customAttr);
45-
4641
// Map the attributes to a key-value object
4742
const attrbs = parseUserAttributes<typeof customAttr>(
4843
user.Attributes,

packages/cognito-import/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "cognito-import",
33
"description": "A simple CLI tool and JavaScript package for importing users into an AWS Cognito user pool.",
4-
"version": "1.2.7",
4+
"version": "1.2.8",
55
"main": "dist/index.js",
66
"bin": "dist/cli.js",
77
"repository": {
@@ -31,6 +31,7 @@
3131
"commander": "^12.0.0",
3232
"node-fetch": "^2.7.0",
3333
"papaparse": "^5.4.1",
34+
"zod": "^3.23.8",
3435
"zod-cognito": "^1.2.6"
3536
},
3637
"devDependencies": {

packages/cognito-import/src/CognitoBase.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CognitoIdentityProvider } from "@aws-sdk/client-cognito-identity-provider";
22
import { IAM } from "@aws-sdk/client-iam";
33
import chalk from "chalk";
4+
import { z } from "zod";
45

56
type CognitoExportProps = {
67
userPoolId: string;
@@ -14,6 +15,7 @@ class CognitoBase {
1415
protected iamArn: string | undefined;
1516
protected userPoolId: string;
1617
protected cognito: CognitoIdentityProvider | undefined;
18+
protected customAttributes: Record<string, z.ZodType<string | number>> = {}
1719

1820
constructor(options: CognitoExportProps) {
1921
this.verbose = !!options.verbose;
@@ -83,6 +85,16 @@ class CognitoBase {
8385
}
8486
this.log("Found user pool " + userPool.UserPool?.Name);
8587

88+
// Set the custom attributes
89+
userPool.UserPool?.SchemaAttributes?.forEach((attr) => {
90+
if (attr.Name && attr.Name.startsWith("custom:")) {
91+
this.customAttributes[attr.Name] = attr.AttributeDataType === "String" ? z.string() : z.number();
92+
}
93+
});
94+
if(Object.keys(this.customAttributes).length) {
95+
this.log('Extracted custom attributes: ' + Object.keys(this.customAttributes).join(','))
96+
}
97+
8698
// Create the iamArn if it doesn't exist
8799
if (!this.iamArn && !!userPool.UserPool?.Arn) {
88100
// arn:aws:cognito-idp:eu-west-1:000000000000:userpool/eu-west-1_XXXXXXX

packages/zod-cognito/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "zod-cognito",
33
"description": "Zod schemas for AWS Cognito User Pools",
4-
"version": "1.2.7",
4+
"version": "1.2.8",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"repository": {

0 commit comments

Comments
 (0)