-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(users): add api endpoint to list users in a team (#65)
- Loading branch information
Showing
4 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { command, flag, flags } from "../../../zcli.ts"; | ||
import { asserts } from "../../../lib/asserts.ts"; | ||
import { dataTable } from "../../../lib/data-table.ts"; | ||
import { loading } from "../../../lib/loading.ts"; | ||
import * as psFlags from "../../../flags.ts"; | ||
import { pickJson } from "../../../lib/pick-json.ts"; | ||
import { config } from "../../../config.ts"; | ||
import { teamUsers } from "../../../api/teams.ts"; | ||
import { defaultFields } from "../mod.ts"; | ||
|
||
/** | ||
* This variable is automatically generated by `zcli add`. Do not remove this | ||
* or change its name unless you're no longer using `zcli add`. | ||
*/ | ||
const subCommands: ReturnType<typeof command>[] = []; | ||
|
||
export const list = command("list", { | ||
short: "List users.", | ||
long: ({ root }) => ` | ||
List users in your team. | ||
Pick a subset of fields to display: | ||
\`\`\` | ||
${root.name} user list -F email -F dtCreated | ||
\`\`\` | ||
`, | ||
commands: subCommands, | ||
flags: psFlags.paginator.merge(flags({ | ||
"team-id": flag({ | ||
aliases: ["t"], | ||
short: "The ID of the team to list users in", | ||
long: ` | ||
The ID of the team to list users in. If not specified, the current team | ||
will be used. | ||
`, | ||
}).ostring(), | ||
})), | ||
// We use command metadata in the `persistentPreRun` function to check if a | ||
// command requires an API key. If it does, we'll check to see if one is | ||
// set. If not, we'll throw an error. | ||
meta: { | ||
requireApiKey: true, | ||
}, | ||
}).run( | ||
async function* ({ flags }) { | ||
let teamId = flags["team-id"]; | ||
if (!teamId) { | ||
const team = await config.get("team"); | ||
asserts(team, "You must be in a team to list users."); | ||
teamId = team; | ||
} | ||
|
||
const result = await loading( | ||
teamUsers.list({ | ||
limit: flags.limit, | ||
after: flags.after, | ||
order: flags.asc ? "asc" : undefined, | ||
teamId, | ||
}), | ||
{ enabled: !flags.json }, | ||
); | ||
|
||
asserts(result.ok, result); | ||
|
||
if (!flags.json) { | ||
for await ( | ||
const line of dataTable( | ||
result.data.items, | ||
flags.fields ?? defaultFields, | ||
) | ||
) { | ||
yield line; | ||
} | ||
} else { | ||
yield pickJson(result.data, flags.fields); | ||
} | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { command } from "../../zcli.ts"; | ||
import { list } from "./list/mod.ts"; | ||
|
||
export const defaultFields = [ | ||
"id", | ||
"teamId", | ||
]; | ||
|
||
/** | ||
* This variable is automatically generated by `zcli add`. Do not remove this | ||
* or change its name unless you're no longer using `zcli add`. | ||
*/ | ||
const subCommands: ReturnType<typeof command>[] = [ | ||
list, | ||
]; | ||
|
||
export const user = command("user", { | ||
short: "Information about users", | ||
long: ` | ||
Show information about users. | ||
`, | ||
commands: subCommands, | ||
}).run(function* ({ ctx }) { | ||
for (const line of user.help(ctx)) { | ||
yield line; | ||
} | ||
}); |