|
| 1 | +import {Args, Flags} from '@oclif/core'; |
| 2 | +import {randomUUID} from 'node:crypto'; |
| 3 | +import { |
| 4 | + SlasClientCommand, |
| 5 | + type Client, |
| 6 | + type ClientOutput, |
| 7 | + parseMultiple, |
| 8 | + normalizeClientResponse, |
| 9 | + printClientDetails, |
| 10 | + formatApiError, |
| 11 | +} from '../../../utils/slas/client.js'; |
| 12 | +import {t} from '../../../i18n/index.js'; |
| 13 | + |
| 14 | +export default class SlasClientCreate extends SlasClientCommand<typeof SlasClientCreate> { |
| 15 | + static args = { |
| 16 | + clientId: Args.string({ |
| 17 | + description: 'SLAS client ID to create or update (generates UUID if omitted)', |
| 18 | + required: false, |
| 19 | + }), |
| 20 | + }; |
| 21 | + |
| 22 | + static description = t('commands.slas.client.create.description', 'Create or update a SLAS client'); |
| 23 | + |
| 24 | + static enableJsonFlag = true; |
| 25 | + |
| 26 | + static examples = [ |
| 27 | + '<%= config.bin %> <%= command.id %> --tenant-id abcd_123 --channels RefArch --scopes sfcc.shopper-products,sfcc.shopper-search --redirect-uri http://localhost:3000/callback', |
| 28 | + '<%= config.bin %> <%= command.id %> my-client-id --tenant-id abcd_123 --name "My Client" --channels RefArch --scopes sfcc.shopper-products --redirect-uri http://localhost:3000/callback --public', |
| 29 | + '<%= config.bin %> <%= command.id %> my-client-id --tenant-id abcd_123 --name "My Client" --channels RefArch --scopes sfcc.shopper-products --redirect-uri http://localhost:3000/callback --json', |
| 30 | + ]; |
| 31 | + |
| 32 | + static flags = { |
| 33 | + ...SlasClientCommand.baseFlags, |
| 34 | + name: Flags.string({ |
| 35 | + description: 'Display name for the client (generates timestamped name if omitted)', |
| 36 | + }), |
| 37 | + channels: Flags.string({ |
| 38 | + description: 'Site IDs/channels (comma-separated or multiple flags)', |
| 39 | + required: true, |
| 40 | + multiple: true, |
| 41 | + }), |
| 42 | + scopes: Flags.string({ |
| 43 | + description: 'OAuth scopes for the client (comma-separated or multiple flags)', |
| 44 | + required: true, |
| 45 | + multiple: true, |
| 46 | + }), |
| 47 | + 'redirect-uri': Flags.string({ |
| 48 | + description: 'Redirect URIs (comma-separated or multiple flags)', |
| 49 | + required: true, |
| 50 | + multiple: true, |
| 51 | + }), |
| 52 | + 'callback-uri': Flags.string({ |
| 53 | + description: 'Callback URIs for passwordless login (comma-separated or multiple flags)', |
| 54 | + multiple: true, |
| 55 | + }), |
| 56 | + secret: Flags.string({ |
| 57 | + description: 'Client secret for private clients (if omitted, one will be generated)', |
| 58 | + }), |
| 59 | + public: Flags.boolean({ |
| 60 | + description: 'Create a public client (default is private)', |
| 61 | + default: false, |
| 62 | + }), |
| 63 | + }; |
| 64 | + |
| 65 | + async run(): Promise<ClientOutput> { |
| 66 | + this.requireOAuthCredentials(); |
| 67 | + |
| 68 | + const { |
| 69 | + 'tenant-id': tenantId, |
| 70 | + name, |
| 71 | + channels, |
| 72 | + scopes, |
| 73 | + 'redirect-uri': redirectUri, |
| 74 | + 'callback-uri': callbackUri, |
| 75 | + secret, |
| 76 | + public: isPublic, |
| 77 | + } = this.flags; |
| 78 | + |
| 79 | + // Use provided client ID or generate a UUID |
| 80 | + const clientId = this.args.clientId ?? randomUUID().toLowerCase(); |
| 81 | + |
| 82 | + // Use provided name or generate a timestamped name |
| 83 | + const clientName = name ?? `b2c-cli client ${new Date().toISOString()}`; |
| 84 | + |
| 85 | + const parsedChannels = parseMultiple(channels); |
| 86 | + const parsedScopes = parseMultiple(scopes); |
| 87 | + const parsedRedirectUri = parseMultiple(redirectUri); |
| 88 | + const parsedCallbackUri = callbackUri ? parseMultiple(callbackUri) : undefined; |
| 89 | + |
| 90 | + if (!this.jsonEnabled()) { |
| 91 | + this.log(t('commands.slas.client.create.creating', 'Creating/updating SLAS client {{clientId}}...', {clientId})); |
| 92 | + } |
| 93 | + |
| 94 | + const slasClient = this.getSlasClient(); |
| 95 | + |
| 96 | + // eslint-disable-next-line new-cap |
| 97 | + const {data, error, response} = await slasClient.PUT('/tenants/{tenantId}/clients/{clientId}', { |
| 98 | + params: { |
| 99 | + path: {tenantId, clientId}, |
| 100 | + }, |
| 101 | + body: { |
| 102 | + clientId, |
| 103 | + name: clientName, |
| 104 | + channels: parsedChannels, |
| 105 | + scopes: parsedScopes, |
| 106 | + redirectUri: parsedRedirectUri, |
| 107 | + callbackUri: parsedCallbackUri, |
| 108 | + // For private clients, use provided secret or generate one with sk_ prefix |
| 109 | + // For public clients, secret is ignored but still required by the schema |
| 110 | + secret: secret ?? (isPublic ? '' : `sk_${randomUUID().replaceAll('-', '')}`), |
| 111 | + isPrivateClient: !isPublic, |
| 112 | + }, |
| 113 | + }); |
| 114 | + |
| 115 | + if (error) { |
| 116 | + this.error( |
| 117 | + t('commands.slas.client.create.error', 'Failed to create/update SLAS client: {{message}}', { |
| 118 | + message: formatApiError(error), |
| 119 | + }), |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + const client = data as Client; |
| 124 | + const wasCreated = response.status === 201; |
| 125 | + |
| 126 | + const output = normalizeClientResponse(client); |
| 127 | + // Use our parsed values as fallback if API doesn't return them |
| 128 | + if (output.scopes.length === 0) output.scopes = parsedScopes; |
| 129 | + if (output.channels.length === 0) output.channels = parsedChannels; |
| 130 | + if (!output.redirectUri) output.redirectUri = parsedRedirectUri.join(', '); |
| 131 | + |
| 132 | + if (this.jsonEnabled()) { |
| 133 | + return output; |
| 134 | + } |
| 135 | + |
| 136 | + // Human-readable output |
| 137 | + if (wasCreated) { |
| 138 | + this.log(t('commands.slas.client.create.created', 'SLAS client created successfully.')); |
| 139 | + } else { |
| 140 | + this.log(t('commands.slas.client.create.updated', 'SLAS client updated successfully.')); |
| 141 | + } |
| 142 | + |
| 143 | + printClientDetails(output); |
| 144 | + |
| 145 | + return output; |
| 146 | + } |
| 147 | +} |
0 commit comments