Skip to content

Commit

Permalink
Merge pull request #848 from appwrite/feat-create-resources
Browse files Browse the repository at this point in the history
Feat create resources
  • Loading branch information
christyjacob4 authored May 30, 2024
2 parents 26054b2 + f0be8ee commit 1d0350b
Show file tree
Hide file tree
Showing 11 changed files with 616 additions and 243 deletions.
5 changes: 5 additions & 0 deletions src/SDK/Language/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ public function getFiles(): array
'destination' => 'lib/utils.js',
'template' => 'cli/lib/utils.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/commands/init.js',
'template' => 'cli/lib/commands/init.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/commands/pull.js',
Expand Down
10 changes: 6 additions & 4 deletions templates/cli/base/params.twig
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@
{% endif %}
{% endfor %}
{% if method.type == 'location' %}
payload['project'] = localConfig.getProject().projectId
payload['key'] = globalConfig.getKey();
const queryParams = new URLSearchParams(payload);
apiPath = `${globalConfig.getEndpoint()}${apiPath}?${queryParams.toString()}`;
if (!overrideForCli) {
payload['project'] = localConfig.getProject().projectId
payload['key'] = globalConfig.getKey();
const queryParams = new URLSearchParams(payload);
apiPath = `${globalConfig.getEndpoint()}${apiPath}?${queryParams.toString()}`;
}
{% endif %}
13 changes: 8 additions & 5 deletions templates/cli/base/requests/api.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
{% endfor %}
}, payload{% if method.type == 'location' %}, 'arraybuffer'{% endif %});

{% if method.type == 'location' %}
fs.writeFileSync(destination, response);
{%~ if method.type == 'location' %}
if (overrideForCli) {
response = Buffer.from(response);
}

{% endif %}
fs.writeFileSync(destination, response);
{%~ endif %}
if (parseOutput) {
parse(response)
success()
}
return response;

return response;
2 changes: 2 additions & 0 deletions templates/cli/index.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { commandDescriptions, cliConfig } = require("./lib/parser");
const { client } = require("./lib/commands/generic");
{% if sdk.test != "true" %}
const { login, logout, whoami } = require("./lib/commands/generic");
const { init } = require("./lib/commands/init");
const { pull } = require("./lib/commands/pull");
const { push } = require("./lib/commands/push");
{% endif %}
Expand All @@ -38,6 +39,7 @@ program
{% if sdk.test != "true" %}
.addCommand(whoami)
.addCommand(login)
.addCommand(init)
.addCommand(pull)
.addCommand(push)
.addCommand(logout)
Expand Down
20 changes: 17 additions & 3 deletions templates/cli/lib/commands/command.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const {{ service.name | caseLower }} = new Command("{{ service.name | caseLower
{% for parameter in method.parameters.all %}
* @property {{ "{" }}{{ parameter | typeName }}{{ "}" }} {{ parameter.name | caseCamel | escapeKeyword }} {{ parameter.description | replace({'`':'\''}) | replace({'\n':' '}) | replace({'\n \n':' '}) }}
{% endfor %}
* @property {boolean} overrideForCli
* @property {boolean} parseOutput
* @property {libClient | undefined} sdk
{% if 'multipart/form-data' in method.consumes %}
Expand All @@ -58,8 +59,21 @@ const {{ service.name | caseLower }} = new Command("{{ service.name | caseLower
/**
* @param {{ "{" }}{{ service.name | caseUcfirst }}{{ method.name | caseUcfirst }}RequestParams{{ "}" }} params
*/
const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ {% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}, {% endfor %}parseOutput = true, sdk = undefined{% if 'multipart/form-data' in method.consumes %}, onProgress = () => {}{% endif %}{% if method.type == 'location' %}, destination{% endif %}}) => {
let client = !sdk ? await {% if service.name == "projects" %}sdkForConsole(){% else %}sdkForProject(){% endif %} : sdk;
{%~ block decleration -%}
const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({
{%- for parameter in method.parameters.all -%}
{{ parameter.name | caseCamel | escapeKeyword }},
{%- endfor -%}

{%- block baseParams -%}parseOutput = true, overrideForCli = false, sdk = undefined {%- endblock -%}

{%- if 'multipart/form-data' in method.consumes -%},onProgress = () => {}{%- endif -%}

{%- if method.type == 'location' -%}, destination{%- endif -%}
}) => {
{%~ endblock %}
let client = !sdk ? await {% if service.name == "projects" %}sdkForConsole(){% else %}sdkForProject(){% endif %} :
sdk;
let apiPath = '{{ method.path }}'{% for parameter in method.parameters.path %}.replace('{{ '{' }}{{ parameter.name | caseCamel }}{{ '}' }}', {{ parameter.name | caseCamel | escapeKeyword }}){% endfor %};
{{ include ('cli/base/params.twig') }}
{% if 'multipart/form-data' in method.consumes %}
Expand Down Expand Up @@ -90,4 +104,4 @@ module.exports = {
{{ service.name | caseLower }}{{ method.name | caseUcfirst }}{% if not loop.last %},{% endif %}

{% endfor %}
};
};
106 changes: 55 additions & 51 deletions templates/cli/lib/commands/generic.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,58 @@ const { actionRunner, success, parseBool, commandDescriptions, error, parse, dra
const { questionsLogin, questionsListFactors, questionsMfaChallenge } = require("../questions");
const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");

const loginCommand = async () => {
const answers = await inquirer.prompt(questionsLogin)

let client = await sdkForConsole(false);

await accountCreateEmailPasswordSession({
email: answers.email,
password: answers.password,
parseOutput: false,
sdk: client
})

client.setCookie(globalConfig.getCookie());

let account;

try {
account = await accountGet({
sdk: client,
parseOutput: false
});
} catch(error) {
if (error.response === 'user_more_factors_required') {
const { factor } = await inquirer.prompt(questionsListFactors);

const challenge = await accountCreateMfaChallenge({
factor,
parseOutput: false,
sdk: client
});

const { otp } = await inquirer.prompt(questionsMfaChallenge);

await accountUpdateMfaChallenge({
challengeId: challenge.$id,
otp,
parseOutput: false,
sdk: client
});

account = await accountGet({
sdk: client,
parseOutput: false
});
} else {
throw error;
}
}

success("Signed in as user with ID: " + account.$id);
};

const whoami = new Command("whoami")
.description(commandDescriptions['whoami'])
.option("-j, --json", "Output in JSON format")
Expand Down Expand Up @@ -48,62 +100,13 @@ const whoami = new Command("whoami")
drawTable(data)
}));


const login = new Command("login")
.description(commandDescriptions['login'])
.configureHelp({
helpWidth: process.stdout.columns || 80
})
.action(actionRunner(async () => {
const answers = await inquirer.prompt(questionsLogin)

let client = await sdkForConsole(false);

await accountCreateEmailPasswordSession({
email: answers.email,
password: answers.password,
parseOutput: false,
sdk: client
})

client.setCookie(globalConfig.getCookie());

let account;

try {
account = await accountGet({
sdk: client,
parseOutput: false
});
} catch (error) {
if (error.response === 'user_more_factors_required') {
const { factor } = await inquirer.prompt(questionsListFactors);

const challenge = await accountCreateMfaChallenge({
factor,
parseOutput: false,
sdk: client
});

const { otp } = await inquirer.prompt(questionsMfaChallenge);

await accountUpdateMfaChallenge({
challengeId: challenge.$id,
otp,
parseOutput: false,
sdk: client
});

account = await accountGet({
sdk: client,
parseOutput: false
});
} else {
throw error;
}
}

success("Signed in as user with ID: " + account.$id);
}));
.action(actionRunner(loginCommand));

const logout = new Command("logout")
.description(commandDescriptions['logout'])
Expand Down Expand Up @@ -199,6 +202,7 @@ const client = new Command("client")

module.exports = {
{% if sdk.test != "true" %}
loginCommand,
whoami,
login,
logout,
Expand Down
Loading

0 comments on commit 1d0350b

Please sign in to comment.