Skip to content

Commit

Permalink
Merge pull request #169 from friggframework/feature/lef-229-migrate-f…
Browse files Browse the repository at this point in the history
…rontify-api-module-over-to-frigg

Feature/lef 229 migrate frontify api module over to frigg
  • Loading branch information
leofmds authored Jun 8, 2023
2 parents 885842f + 3eed3c4 commit e92bc01
Show file tree
Hide file tree
Showing 14 changed files with 7,126 additions and 0 deletions.
99 changes: 99 additions & 0 deletions api-module-library/frontify/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const { get } = require('@friggframework/assertions');
const { OAuth2Requester } = require('@friggframework/module-plugin');
const querystring = require('querystring');

class Api extends OAuth2Requester {
constructor(params) {
super(params);
this.domain = get(params, 'domain', null);

if (this.domain) {
this.baseUrl = `https://${this.domain}/graphql`;
this.tokenUri = `https://${this.domain}/api/oauth/accesstoken`;
}
}

setDomain(domain) {
this.domain = domain;
this.baseUrl = `https://${this.domain}/graphql`;
this.tokenUri = `https://${this.domain}/api/oauth/accesstoken`;
}

getAuthUri() {
const query = {
client_id: this.client_id,
response_type: 'code',
redirect_uri: this.redirect_uri,
scope: this.scope,
state: this.state,
};

let authorizationUri;

if (this.domain) {
authorizationUri = `https://${this.domain}/api/oauth/authorize`;
} else {
authorizationUri = 'https://{{domain}}/api/oauth/authorize';
}

return `${authorizationUri}?${querystring.stringify(query)}`;
}

buildRequestOptions(query) {
return {
url: this.baseUrl,
headers: {
'Content-Type': 'application/json',
},
body: {
query
},
};
}

async getUser() {
const query = 'query CurrentUser { currentUser { id email name }}';
const response = await this._post(this.buildRequestOptions(query));
return {
user: response.data.currentUser,
};
}

async listBrands() {
const ql = 'query Brands { brands { id avatar name }}';
const response = await this._post(this.buildRequestOptions(ql));
return response.data;
}

async listProjects(query) {
const ql = `query Projects { brand(id: "${query.brandId}") { workspaceProjects { items { id name }}}}`;
const response = await this._post(this.buildRequestOptions(ql));
return {
projects: response.data.brand.workspaceProjects.items,
};
}

async listLibraries(query) {
const ql = `query Libraries { brand(id: "${query.brandId}") { libraries { items { id name }}}}`;
const response = await this._post(this.buildRequestOptions(ql));
return response.data.brand.libraries.items;
}

async listProjectAssets(query) {
const ql = `query ProjectAssets { workspaceProject(id: "${query.projectId}") { assets { items { id title description }}}}`;
const response = await this._post(this.buildRequestOptions(ql));
return {
assets: response.data.workspaceProject.assets.items,
};
}

async listLibraryAssets(query) {
const ql = `query LibraryAssets { library(id: "${query.libraryId}") { assets { items { id title description }}}}`;
const response = await this._post(this.buildRequestOptions(ql));
return {
assets: response.data.library.assets.items,
};
}
}

module.exports = { Api };
Loading

0 comments on commit e92bc01

Please sign in to comment.