@contember/engine-s3-plugin 2.0.0-alpha.36 → 2.0.0-alpha.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contember/engine-s3-plugin",
3
- "version": "2.0.0-alpha.36",
3
+ "version": "2.0.0-alpha.38",
4
4
  "license": "Apache-2.0",
5
5
  "main": "./dist/production/index.js",
6
6
  "typings": "./dist/types/index.d.ts",
@@ -23,9 +23,9 @@
23
23
  }
24
24
  },
25
25
  "dependencies": {
26
- "@contember/engine-plugins": "2.0.0-alpha.36",
27
- "@contember/graphql-utils": "2.0.0-alpha.36",
28
- "@contember/typesafe": "2.0.0-alpha.36",
26
+ "@contember/engine-plugins": "2.0.0-alpha.38",
27
+ "@contember/graphql-utils": "2.0.0-alpha.38",
28
+ "@contember/typesafe": "2.0.0-alpha.38",
29
29
  "mime-types": "^2.1.35",
30
30
  "picomatch": "^4.0.2",
31
31
  "qs": "^6.13.0",
@@ -1,4 +1,5 @@
1
1
  import {
2
+ GraphQLError,
2
3
  GraphQLFieldConfig,
3
4
  GraphQLInt,
4
5
  GraphQLNonNull,
@@ -8,7 +9,7 @@ import {
8
9
  GraphQLString,
9
10
  } from 'graphql'
10
11
  import { S3Service, S3ServiceFactory } from './S3Service'
11
- import { resolveS3Config, S3Config } from './Config'
12
+ import { Project3Config, resolveS3Config, S3Config } from './Config'
12
13
  import { GraphQLSchemaContributor, GraphQLSchemaContributorContext, Providers } from '@contember/engine-plugins'
13
14
  import * as types from './S3SchemaTypes'
14
15
  import { S3Acl, S3GenerateSignedUploadInput, S3SignedRead, S3SignedUpload } from './S3SchemaTypes'
@@ -35,13 +36,16 @@ export type S3SchemaAcl = Record<
35
36
 
36
37
  export class S3SchemaContributor implements GraphQLSchemaContributor {
37
38
  constructor(
38
- private readonly s3Config: S3Config | undefined,
39
39
  private readonly s3Factory: S3ServiceFactory,
40
40
  private readonly providers: Providers,
41
41
  ) {}
42
42
 
43
+ getCacheKey?({ project }: GraphQLSchemaContributorContext): string {
44
+ return project.s3 ? 'yes' : 'no'
45
+ }
46
+
43
47
  createSchema(context: GraphQLSchemaContributorContext): GraphQLSchemaConfig | undefined {
44
- if (!this.s3Config) {
48
+ if (!context.project.s3) {
45
49
  return undefined
46
50
  }
47
51
  const rules = context.identity.projectRoles.flatMap(it =>
@@ -61,11 +65,9 @@ export class S3SchemaContributor implements GraphQLSchemaContributor {
61
65
  return undefined
62
66
  }
63
67
 
64
- const s3Config = resolveS3Config(this.s3Config)
65
68
  const authorizator = new S3ObjectAuthorizator(uploadRules, readRules)
66
- const s3 = this.s3Factory.create(s3Config, this.providers, authorizator)
67
- const uploadMutation = this.createUploadMutation(s3)
68
- const readMutation = this.createReadMutation(s3)
69
+ const uploadMutation = this.createUploadMutation(authorizator)
70
+ const readMutation = this.createReadMutation(authorizator)
69
71
  const mutation = {
70
72
  generateUploadUrl: uploadMutation as GraphQLFieldConfig<any, any, any>,
71
73
  generateReadUrl: readMutation,
@@ -86,7 +88,8 @@ export class S3SchemaContributor implements GraphQLSchemaContributor {
86
88
  }
87
89
  }
88
90
 
89
- private createReadMutation(s3: S3Service): GraphQLFieldConfig<any, any, any> {
91
+ private createReadMutation(authorizator: S3ObjectAuthorizator): GraphQLFieldConfig<any, { project: Project3Config }, any> {
92
+
90
93
  return {
91
94
  type: new GraphQLNonNull(S3SignedRead),
92
95
  args: {
@@ -97,13 +100,18 @@ export class S3SchemaContributor implements GraphQLSchemaContributor {
97
100
  type: GraphQLInt,
98
101
  },
99
102
  },
100
- resolve: async (parent: any, args: { objectKey: string; expiration?: number }) => {
103
+ resolve: async (parent: any, args: { objectKey: string; expiration?: number }, ctx: { project: Project3Config }) => {
104
+ if (!ctx.project.s3) {
105
+ throw new GraphQLError('S3 is not configured for this project')
106
+ }
107
+ const s3Config = resolveS3Config(ctx.project.s3)
108
+ const s3 = this.s3Factory.create(s3Config, this.providers, authorizator)
101
109
  return s3.getSignedReadUrl({ objectKey: args.objectKey, expiration: args.expiration ?? null })
102
110
  },
103
111
  } as GraphQLFieldConfig<any, any, any>
104
112
  }
105
113
 
106
- private createUploadMutation(s3: S3Service): GraphQLFieldConfig<any, any, any> {
114
+ private createUploadMutation(authorizator: S3ObjectAuthorizator): GraphQLFieldConfig<any, any, any> {
107
115
  return {
108
116
  type: new GraphQLNonNull(S3SignedUpload),
109
117
  args: {
@@ -116,7 +124,13 @@ export class S3SchemaContributor implements GraphQLSchemaContributor {
116
124
  resolve: async (
117
125
  parent: any,
118
126
  args: { input?: Partial<S3GenerateSignedUploadInput>; contentType?: string; acl?: S3Acl; expiration?: number; prefix?: string },
127
+ ctx: { project: Project3Config },
119
128
  ) => {
129
+ if (!ctx.project.s3) {
130
+ throw new GraphQLError('S3 is not configured for this project')
131
+ }
132
+ const s3Config = resolveS3Config(ctx.project.s3)
133
+ const s3 = this.s3Factory.create(s3Config, this.providers, authorizator)
120
134
  return s3.getSignedUploadUrl({
121
135
  acl: args.input?.acl ?? args.acl ?? null,
122
136
  contentDisposition: args.input?.contentDisposition ?? null,
package/src/index.ts CHANGED
@@ -15,11 +15,8 @@ export default class S3 implements Plugin<Project3Config> {
15
15
  return new S3ConfigProcessor()
16
16
  }
17
17
 
18
- getSchemaContributor({ project, providers }: SchemaContributorArgs<Project3Config>) {
19
- if (!project.s3) {
20
- return undefined
21
- }
18
+ getSchemaContributor({ providers }: SchemaContributorArgs) {
22
19
  const s3ServiceFactory = new S3ServiceFactory()
23
- return new S3SchemaContributor(project.s3, s3ServiceFactory, providers)
20
+ return new S3SchemaContributor(s3ServiceFactory, providers)
24
21
  }
25
22
  }