Comments (5)
If you don't want to install any additional dependencies you can use nextjs env library directly
const { loadEnvConfig } = require("@next/env");
const contentful = require("contentful-management");
module.exports = async function() {
loadEnvConfig(process.env.PWD);
const client = contentful.createClient({
accessToken: process.env.CONTENTFUL_MANAGEMENT_TOKEN,
})
return client
.getSpace(process.env.CONTENTFUL_SPACE_ID)
.then(space => space.getEnvironment(process.env.CONTENTFUL_ENVIRONMENT))
}
from contentful-typescript-codegen.
For those who on windows. Install dotenv
and then add the config injection like this:
const contentfulManagement = require('contentful-management')
require('dotenv').config() // <-- here is the trick
module.exports = function () {
const contentfulClient = contentfulManagement.createClient({
accessToken: process.env.PRIVATE_CONTENTFUL_MANAGEMENT_ACCESS_TOKEN,
})
return contentfulClient
.getSpace(process.env.PRIVATE_CONTENTFUL_SPACE_ID)
.then((space) =>
space.getEnvironment(process.env.PRIVATE_CONTENTFUL_ENVIRONMENT_ID)
)
}
from contentful-typescript-codegen.
Altough, even .env
file doesn't work with your library on windows.
from contentful-typescript-codegen.
Hello, I am having an issue with the Image component from Next and typescript. I have types like this
// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
import { Asset, Entry } from "contentful";
export interface ITheyTrustUsCompaniesFields {
/** name */
name?: string | undefined;
/** icon */
icon?: Asset | undefined;
}
/** These are the companies that trust *** with building complex web and mobile applications. */
export interface ITheyTrustUsCompanies
extends Entry<ITheyTrustUsCompaniesFields> {
sys: {
id: string;
type: string;
createdAt: string;
updatedAt: string;
locale: string;
contentType: {
sys: {
id: "theyTrustUsCompanies";
linkType: "ContentType";
type: "Link";
};
};
};
}
export type CONTENT_TYPE = "theyTrustUsCompanies";
export type IEntry = ITheyTrustUsCompanies;
export type LOCALE_CODE = "en-US";
export type CONTENTFUL_DEFAULT_LOCALE_CODE = "en-US";
But I need to do "as string" when I want to use image the asset like so, I would like to not have to do this on every single image, right?
<Image
src={item.icon?.fields.file?.url as string}
alt={item.icon?.fields.description as string}
width="200"
height="85"
/>
The error message I get if I don't is: Type 'string | AssetFile | undefined' is not assignable to type 'string | StaticImport'. but I cannot modify the generated file to change the type, how should I go about this? I'm asking here because it is related to NextJS, I hope that is fine, I can create a stack overflow question or another issue with no problems.
Thanks
from contentful-typescript-codegen.
That's not a NextJS issue, your types looks fine.
I think it's because your icon is optional in contentful, hence the URL to the icon image can be undefined
.
If you cannot change this then you need to check if it's set before loading your image
const icon = item.icon?.fields.file?.url;
if (!icon) {
return null;
}
return <Image
src={icon}
alt={item.icon?.fields.description || 'no description'}
width="200"
height="85"
/>
from contentful-typescript-codegen.
Related Issues (20)
- The automated release is failing 🚨 HOT 2
- TypeError: Cannot read property 'getContentTypes' of undefined HOT 6
- Imports { Document } when it is not used HOT 2
- Feature Request: Export union of all entries HOT 1
- Fields are incorrectly extending Entry HOT 7
- ERR_REQUIRE_ESM in Node 16.6 HOT 5
- Not using prettierrc.json
- Missing collection types in generated file HOT 3
- Type narrowing not working with current setup HOT 1
- generating typescript models fails for specific space with TypeError: contentTypeValidation.linkContentType.map is not a function
- Syntax error from code generation
- TypeError: functionMap[field.type] is not a function HOT 2
- Update to work with Contentful v10 HOT 8
- Non-required attributes are not automatically generated to have `| null` appended to type
- getContenfulEnvironment.ts HOT 1
- Prettier config not resolved correctly
- contentful^8.1.9 is required HOT 1
- Top level id, contentType and type on Entry HOT 1
- have option to add namespace and own commenting to generated types
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from contentful-typescript-codegen.