Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SFS-2225] refactor: code refactor with updates and rebuild #11

Merged
merged 9 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
coverage/
*.snap.ts
9 changes: 9 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "vtex",
"root": true,
"env": {
"node": true,
"es6": true,
"jest": true
}
}
7 changes: 1 addition & 6 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
{
"semi": false,
"singleQuote": true,
"trailingComma": "es5",
"eslintIntegration": true
}
"@vtex/prettier-config"
15 changes: 15 additions & 0 deletions .vtexignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.DS_Store
.git/
.happypack/
.vscode/
node_modules/
.editorconfig
.eslintrc
.gitignore
gulpfile.js
package.json
tsconfig.json
tslint.json
typings.d.ts
yarn.lock
node/node_modules/
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.4.0] - 2025-02-18

### Fixed
- Refactor code for proper build
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "file-manager-graphql",
"vendor": "vtex",
"version": "0.3.0",
"version": "0.4.0-beta.1",
"title": "File Manager GraphQL",
"description": "",
"policies": [
Expand All @@ -17,7 +17,7 @@
},
"builders": {
"graphql": "1.x",
"node": "3.x",
"node": "6.x",
"docs": "0.x"
},
"credentialType": "absolute",
Expand Down
92 changes: 62 additions & 30 deletions node/FileManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {HttpClient, IOContext, InstanceOptions} from '@vtex/api'
import {FileNotFound} from './exceptions/fileNotFound'
import {InternalServerError} from './exceptions/internalServerError'
import {pathEq, path, pick} from 'ramda'
import type { InstanceOptions, IOContext } from '@vtex/api'
import { ExternalClient } from '@vtex/api'

import { FileNotFound } from './exceptions/fileNotFound'
import { InternalServerError } from './exceptions/internalServerError'

const appId = process.env.VTEX_APP_ID
const [runningAppName] = appId ? appId.split('@') : ['']
Expand All @@ -10,28 +11,53 @@ const FORWARD_FIELDS = ['status', 'statusText', 'data', 'stack', 'stackTrace']

const routes = {
Assets: () => `/assets/${runningAppName}`,
FileUpload: (bucket: string, path: string) => `${routes.Assets()}/save/${bucket}/${path}`,
FileUrl: (bucket: string, path: string) => `${routes.Assets()}/route/${bucket}/${path}`,
FileDelete: (bucket: string, path: string) => `${routes.Assets()}/delete/${bucket}/${path}`,
File: (path: string, width: number, height: number, aspect: boolean, bucket: string) => `${routes.Assets()}/${bucket}/${path}?width=${width}&height=${height}&aspect=${aspect}`
FileUpload: (bucket: string, path: string) =>
`${routes.Assets()}/save/${bucket}/${path}`,
FileUrl: (bucket: string, path: string) =>
`${routes.Assets()}/route/${bucket}/${path}`,
FileDelete: (bucket: string, path: string) =>
`${routes.Assets()}/delete/${bucket}/${path}`,
File: (
path: string,
width: number,
height: number,
aspect: boolean,
bucket: string
) =>
`${routes.Assets()}/${bucket}/${path}?width=${width}&height=${height}&aspect=${aspect}`,
}

export default class FileManager {
private http: HttpClient

constructor (ioContext: IOContext, opts: InstanceOptions = {}) {
if (runningAppName === '') {
throw new InternalServerError(`Invalid path to access FileManger. Variable VTEX_APP_ID is not available.`)
}
this.http = HttpClient.forWorkspace('file-manager.vtex', ioContext, opts)
export default class FileManager extends ExternalClient {

constructor(protected context: IOContext, options?: InstanceOptions) {
super(
`https://app.io.vtex.com/vtex.file-manager/v0/${context.account}/${context.workspace}`,
context,
{
...(options ?? {}),
headers: {
...(options?.headers ?? {}),
'Content-Type': 'application/json',
'X-Vtex-Use-Https': 'true',
},
}
)
}

getFile = async (path: string, width: number, height: number, aspect: boolean, bucket: string) => {
getFile = async (
path: string,
width: number,
height: number,
aspect: boolean,
bucket: string
) => {
try {
return await this.http.get(routes.File(path, width, height, aspect, bucket))
return await this.http.get(
routes.File(path, width, height, aspect, bucket)
)
} catch (e) {
if (e.statusCode === 404 || pathEq(['response', 'status'], 404, e)) {
throw new FileNotFound(pick(FORWARD_FIELDS, e.response))
if (e.statusCode === 404 || e.pathEq(404, ['response', 'status'])) {
throw new FileNotFound(e.response.pick(FORWARD_FIELDS))
} else {
throw e
}
Expand All @@ -40,27 +66,33 @@ export default class FileManager {

getFileUrl = async (path: string, bucket: string) => {
try {
return await this.http.get(routes.FileUrl(bucket, path))
const fileUrl = routes.FileUrl(bucket, path)
const file = await this.http.get(fileUrl)
return file
} catch (e) {
if (e.statusCode === 404 || pathEq(['response', 'status'], 404, e)) {
throw new FileNotFound(pick(FORWARD_FIELDS, e.response))
if (e.statusCode === 404) {
throw new FileNotFound(e.response.pick(FORWARD_FIELDS))
} else {
throw e
}
}
}

saveFile = async (file: IncomingFile, stream, bucket: string) => {
saveFile = async (file: IncomingFile, stream: any, bucket: string) => {
try {
const {filename, encoding, mimetype} = file
const { filename, encoding, mimetype } = file
const headers = {
'Content-Type': mimetype,
'Content-Encoding': encoding,
}
return await this.http.put(routes.FileUpload(bucket, filename), stream, {headers})

return await this.http.put(routes.FileUpload(bucket, filename), stream, {
headers,
})
} catch (e) {
const status = e.statusCode || path(['response', 'status'], e) || 500
const extensions = pick(FORWARD_FIELDS, e.response)
const status = e.statusCode || e.path(['response', 'status']) || 500
const extensions = e.response.pick(FORWARD_FIELDS)

throw new InternalServerError(extensions, 'Fail to save file', status)
}
}
Expand All @@ -69,8 +101,8 @@ export default class FileManager {
try {
return await this.http.delete(routes.FileDelete(bucket, path))
} catch (e) {
if (e.statusCode === 404 || pathEq(['response', 'status'], 404, e)) {
throw new FileNotFound(pick(FORWARD_FIELDS, e.response))
if (e.statusCode === 404 || e.pathEq([404, 'response', 'status'])) {
throw new FileNotFound(e.response.pick(FORWARD_FIELDS))
} else {
throw e
}
Expand Down
10 changes: 10 additions & 0 deletions node/clients/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IOClients } from '@vtex/api'

import Status from './status'

// Extend the default IOClients implementation with our own custom clients.
export class Clients extends IOClients {
public get status() {
return this.getOrSet('status', Status)
}
}
32 changes: 32 additions & 0 deletions node/clients/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { InstanceOptions, IOContext, IOResponse } from '@vtex/api'
import { ExternalClient } from '@vtex/api'

export default class Status extends ExternalClient {
constructor(context: IOContext, options?: InstanceOptions) {
super('http://httpstat.us', context, options)
}

public async getStatus(status: number): Promise<string> {
return this.http.get(status.toString(), {
metric: 'status-get',
})
}

public async getStatusWithHeaders(
status: number
): Promise<IOResponse<string>> {
return this.http.getRaw(status.toString(), {
metric: 'status-get-raw',
})
}

public async getStatusAndForceMaxAge(
status: number
): Promise<IOResponse<string>> {
return this.http.get(status.toString(), {
// when using an LRUCache, this will force the response to be cached
forceMaxAge: 5000,
metric: 'status-get-forceMaxAge',
})
}
}
4 changes: 2 additions & 2 deletions node/exceptions/fileNotFound.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export class FileNotFound extends Error {
constructor(
public extensions,
public extensions: any,
public message = 'File Not Found',
public statusCode = 404,
public statusCode = 404
) {
super(message)
}
Expand Down
4 changes: 2 additions & 2 deletions node/exceptions/internalServerError.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export class InternalServerError extends Error {
constructor(
public extensions,
public extensions: any,
public message = 'Internal Server Error',
public statusCode = 500,
public statusCode = 500
) {
super(message)
}
Expand Down
21 changes: 18 additions & 3 deletions node/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import {resolvers} from './resolvers'
import { resolvers } from './resolvers'
import { Clients } from './clients'
import { ClientsConfig, Service } from '@vtex/api'

export default {
graphql: resolvers
const clients: ClientsConfig<Clients> = {
implementation: Clients,
options: {
default: {
retries: 2,
timeout: 2000,
},
}
}

export default new Service({
clients,
graphql: {
resolvers
}
})
17 changes: 12 additions & 5 deletions node/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
{
"name": "vtex.product-review-graphql-example",
"name": "vtex.file-manager-graphql",
"description": "GraphQL resolvers. Any required dependencies should be defined in this file.",
"version": "0.3.0",
"dependencies": {
"@vtex/api": "^0.40.0",
"@vtex/api": "6.48.0",
"apollo-upload-server": "^5.0.0",
"ramda": "^0.25.0",
"uuid": "^8.2.0"
},
"resolutions": {
"@types/express": "4.16.0",
"@types/express-serve-static-core": "4.16.0"
},
"devDependencies": {
"@types/bluebird-global": "^3.5.12",
"@types/jest": "^24.0.24",
"@types/node": "^10.3.2",
"@types/random-js": "^1.0.30"
"@types/node": "^12.0.0",
"@types/uuid": "^10.0.0",
"tslint": "^5.14.0",
"tslint-config-vtex": "^2.1.0",
"typescript": "3.9.7",
"vtex.file-manager": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.file-manager@0.11.0/public/_types/react"
},
"scripts": {
"lint": "tsc --noEmit && tslint -c tslint.json './**/*.ts'"
Expand Down
Loading
Loading